Skip to content

Template calling methods

Jorge Castro edited this page Dec 3, 2020 · 2 revisions

Calling methods

Service Inject

Tag Note
@inject('metrics', 'App\Services\MetricsService') Used for insert a Laravel Service

@inject('variable name', 'namespace')

@inject('metric', 'App\Services\MetricsService')
<div>
    Monthly Revenue: {{ $metric->monthlyRevenue() }}.
</div>

By default, BladeOne creates a new instance of the class 'variable name' inside 'namespace' with a parameter-less constructor.

To override the logic used to resolve injected classes, pass a function to setInjectResolver.

Example with Symphony Dependency Injection.

$containerBuilder = new ContainerBuilder();
$loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
$loader->load('services.xml');

$blade->setInjectResolver(function ($namespace, $variableName) use ($loader) {
    return $loader->get($namespace);
});

@use(namespace)

It works exactly like the command "use" of PHP.

@use(\namespace1\namespace2)