是否可以将路由参数(或路由段)注入控制器构造函数?
你找到一些代码来澄清我的问题.
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 be some magic Route::get('foo/{bar}','TestController');
不可能注射它们,但您可以通过以下方式访问所有这些:
class TestController{ protected $_param; public function __construct() { $id = Route::current()->getParameter('id'); } }