php - Custom BaseController in Silex -
i have created simple aplication in silex 1.3.4 , want have base controller have __construct method accepting $app , $request. inheriting controllers should have respective constructors , calling parent controller construct method.
//use statements here.... class appcontroller { public function __construct(application $app, request $request){ $this->app = $app; $this->request = $request; } }
inheriting controllers written below:
//use statements here.... class itemscontroller extends appcontroller { public function __construct(application $app, request $request){ parent::__construct($app, $request); } public function listaction() { //code here without having pass application , request objects } }
the approach have decided on routing shown below:
$app->post( '/items/list', 'mysilextestdrive\controller\itemscontroller::listaction' )->bind('list');
i thinking of using dispatcher , override processes there , create controller instances own way not have idea how , if great idea @ all.
anyone has done similar this? please help.
you can use servicecontrollerserviceprovider define controller service in application. can't inject request
in way. btw can have more 1 request , request instance can change if sub-request. can inject requeststack
instead, call $requeststack->getcurrentrequest()
when need current request.
$app = new silex\application(); abstract class appcontroller { protected $app; protected $requeststack; public function __construct(silex\application $app, symfony\component\httpfoundation\requeststack $requeststack) { $this->app = $app; $this->requeststack = $requeststack; } public function getrequest() { return $this->requeststack->getcurrentrequest(); } } class itemscontroller extends appcontroller { public function listaction() { $request = $this->getrequest(); // ... } } $app->register(new silex\provider\servicecontrollerserviceprovider()); $app['items.controller'] = $app->share(function() use ($app) { return new itemscontroller($app, $app['request_stack']); }); $app->get('/items/list', "items.controller:listaction");
it makes sense such thing? i not think so. if framework gives request instance type hinting. do
public function listaction(application $app, request $request) { // ... }
and work that.
Comments
Post a Comment