D
D
dimainc2016-03-01 06:06:02
Laravel
dimainc, 2016-03-01 06:06:02

Calling a helper in a Laravel template?

I am learning Laravel. There was such a question.
Is it possible to call a service provider from a Laravel template?
I found the following fragment in the documentation:
"You can insert into the template any class that is defined in the service container of the framework. For this, the @inject directive is used, the first argument of which is the name of the variable in which the class instance will be placed, and the second is the name of the injected class or interface"
And the example itself:
@inject('metrics', 'App\Services\MetricsService')
Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
in the inject I specified the path to my service provider App\Providers\RatesProvider
But I get an error
Unresolvable dependency resolving [Parameter #0 [ $app ]] in class Illuminate\Support\ServiceProvider (View: D:\OpenServer\domains\myblog.ru\resources\views\admin\edit-post.blade.php
) simple, just returns a specific value when called from a controller it works.
Here is the register function in the service provider

public function register(){
        $this->app->bind('App\Helpers\RatesContract', function () {
            return new RatesController($this->app);
        });
    }

The RactContract.php interface itself
<?php

namespace App\Helpers;

Interface RatesContract
{

    public function getRate();


}

And controller RatesController.php
<?php

namespace App\Helpers;

use App\Helpers\RatesContract;

class RatesController implements RatesContract
{

    public function getRate(){
        
        return "123123";

    }
}

With a normal call and a controller, it returns my value to me

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-03-01
@dimainc

It is not the provider that needs to be injected, but the RatesContract interface itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question