M
M
Mr Freeman2015-08-11 10:35:56
Silex
Mr Freeman, 2015-08-11 10:35:56

How to organize controllers in Silex?

I did first through their services

class StatsController{

    protected $app;

    function __construct(Application $app){

        $this->model = new Stats($this->app);

        if(UserHelper::isOnlySalesManager($this->app)){
            $this->model->isSalesManager  = true;
        }
    }
}

Then I had to call each controller separately in init.php, passing an application instance to the constructor.
This needs to be fixed so that they load themselves.
In the routes then I prescribe the route indicating the action
$admin->get('/stat', 'Controllers\\StatsController::dailyAction')

Then everything would be fine, but $app will no longer be available in the controller constructor - which will lead to a reorganization of the structure. Maybe there are ways how you can get an application instance in the controller constructor without using controllers as services? To pull out common pieces of code there already. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-08-11
Protko @Fesor

silex.sensiolabs.org/doc/providers/service_control...

M
miy, 2015-08-11
@miy

In Slim framework I used it like this:

$app->get('/main/:param', function ($param) use($app) {
     (new \Controllers\Main($app))->index($param);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question