Additional route routes in Silex

I made some views and cannot figure out how to have an optional parameter in the URL segment in Silex. So, I have this route now:

    /{controller}/{method}/{param}

The param wildcard is what I would like to be optional. Thus, this pattern will receive URLs such as

    "Controller1/Method1" and "Controller2/Method2/Param"

Suggestions?

+4
source share
1 answer

Just configure the processor for the longest URL (with all parts, including optional), for example:

$app->get('/controller/{method}/{param}', 
    function($method, $param) {
      // called both by `/controller/some-method/some-param-string`,
      // `/controller/some-other-method`, and even `/controller`
});

, URL-. , :

$app->get('/page/{pageName}', function($pageName) {
  // ...
})->value('pageName', 'index');

, /page, $pageName 'index'.

+11

Source: https://habr.com/ru/post/1534583/


All Articles