Enter constructor along the route

I have a class that I inject into the controller along with the route parameter. Then I use setter to set the route parameter in the class.

routes

Route::get('path/of/url/with/{paramVar}', 'testController@testFunc)

controller

class testController
{
    public function testFunc(MyClassInterface $class, $routeParamVar)
    {
        $class->setParam($routeParamVar);
        //do stuff here
        ...

service provider

public function register()
{
    $this->bind('path\to\interface', 'path\to\concrete');
}

Instead, I would like to enter a route parameter into the constructor of the class that I inject into my controller. I know from this question that I need to use a laravel container.

I can enter other route parameters using Request::class, but how can I enter a route route parameter?

I guess I will get something like this

class testController
{
    public function testFunc(MyClassInterface $class)
    {
        //do stuff here
        ...
+4
source share
1 answer

$router->input('foo') .

https://laravel.com/api/master/Illuminate/Routing/Router.html#method_input

, :

public function register()
{
    $this->bind('path\to\interface', function(){

        $param = $this->app->make('router')->input('foo');

        return new path\to\concrete($param);

    });
}

, , , - FooValueServiceProvider, , FooValueServiceProvider . , , , .

, , , .

+4

Source: https://habr.com/ru/post/1627755/


All Articles