Is it possible to have conditional routing in a Silex application?

I am writing a web application with a front-oriented website and then an admin console. I would like to be able to have a parameter which, when set to true, means that the preview or maintenance page is displayed on the front-end website.

The configuration of my routes is currently in the yaml file and is read on every request. But now I want him to be smart enough to find out if he is in maintenance mode or not, and if he directs all the routes to one specific page. Or it can change routes, so there is only one.

I thought that this can be done with different files loaded based on this parameter, but then means that all routes are static and cannot be extracted from the database, for example. In addition, I had problems reading from the database during the query setup phase. I configured the system to read from the database as a service, but this, it seems, cannot be used at the setup stage, do I understand?

Any pointers gratefully received.

Russell

+4
source share
1 answer

I often use the maintenance page with Silex:

In the same place, I define. $app['debug'] = true;I also define another variable $app['maintenance'] = true;that I use for various checks.

Among them, I define the service page as follows:

$app->before(function (Request $request, Application $app) {
    if($app['maintenance']){
        $subRequest = Request::create('/maintenance', 'GET');
        return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
    }
});

$app->get('/maintenance', function () use ($app) {
    // Here you can return your maintenance page
    return $app->render('maintenance.twig');
});

, , .

, - yaml , .

+6

All Articles