php - Is it possible to pass a route parameter to controller constructor in Laravel? -
is possible inject route-paramter (or route segment) controller-constructor?
you find code clarify question.
class testcontroller{ protected $_param; public function __construct($paramfromroute) { $this->param = $paramfromroute; } public function testaction() { return "hello ".$this->_param; } } ---------------------------------------------------- app::bind('testcontroller', function($app, $paramfromroute){ $controller = new testcontroller($paramfromroute); return $controller; }); ---------------------------------------------------- // here should magic route::get('foo/{bar}', 'testcontroller');
it's not possible inject them, have access of them via:
class testcontroller{ protected $_param; public function __construct() { $id = route::current()->getparameter('id'); } }
Comments
Post a Comment