B
B
Bafisto2017-07-25 22:52:53
PHP
Bafisto, 2017-07-25 22:52:53

Why is there no controllers folder in silex framework?

Hello.
Tell me please, where should controllers and models be stored in this framework?
Or are the controllers simply split into separate files (along with their respective routes) and then simply included in controllers.php?
Tell me please. I just installed silex, and I understand

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor, 2017-07-26
@TexElless

Silex does not have a strict structure out of the box, we have a project with routing right in index.php in our production, and inside the routes, the methods of the necessary services simply twitch. Something like this:

$forecast = $app['controllers_factory'];
$forecast->get('/calculations', function (Request $request) use ($app) {
    $data = new \App\Services\ForecastService($app['db'], $request, $app['user']);
    $return = $data->getForecastCalculations();

    return new JsonResponse((array)$return, 200, ['access-control-allow-origin' => '*']);
});
$app->mount('/forecast', $forecast);
$app->after($app['cors']);

$app->run();

If you need to complicate the logic, you can call the desired controller from the routing, inside it you can already pull the required number of services that prepare the response. At the same time, where services, controllers, models will be stored and whether you will have them at all is entirely up to you. You can do it by analogy with other frameworks (Laravel-style, Symfony-style), you can come up with something of your own, the main thing is to maintain uniformity so that you don’t get confused later.

Y
Yuri, 2017-07-26
@riky

it is expected that there will be few of them. Therefore, either in one file or include.
if you are going to write a dozen controllers and the same number of models, then take laravel, the same microframework, only everything is better organized and more functions.
(I do not recommend the lumen microframework, there are minimal differences from the full laravel)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question