laravel - Constructor injection of route parameter -


i have class injecting controller along route parameter. using setter set route parameter in 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'); } 

i instead inject route parameter constructor of class injecting controller. know from question need use laravel container.

i can inject other route parameters using request::class, how can inject route path parameter?

i guess end this

class testcontroller {     public function testfunc(myclassinterface $class)     {         //do stuff here         ... 

you can use $router->input('foo') function retrieve route parameter within service container.

https://laravel.com/api/master/illuminate/routing/router.html#method_input

so in service provider:

public function register() {     $this->bind('path\to\interface', function(){          $param = $this->app->make('router')->input('foo');          return new path\to\concrete($param);      }); } 

in regards comment, wouldn't reduce code much, might best in case make second service provider, foovalueserviceprovider who's implementation's job retrieve parameter router. in each of bindings resolve foovalueserviceprovider , retrieve value that. later if change name of route param, or need resolve somewhere other route, need change out implementation of provider.

i don't know if can more efficient 1 line of code per binding, @ least way can changed out different method down line.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -