Flexible dynamic routing with Silex

前端 未结 2 732
情书的邮戳
情书的邮戳 2021-01-13 23:57

Is it possible to have an unknown number of arguments for a get request?

Example, this works but isn\'t ideal.

$app->get(\'/print/{template}/{arg1         


        
2条回答
  •  野的像风
    2021-01-14 00:23

    If you really want to, you can easily work around this limitation by relaxing the requirement on the variable. You can do that by using assert:

    $app->get('/pdf/{template}/{args}', function ($template, $args) {
        ...
    })
    ->assert('args', '.*')
    ->convert('args', function ($args) {
        return explode('/', $args);
    });
    

    By making the $args regex more permissive, it will match the rest of the string, even if it includes slashes. The param converter then splits that matched string into segments.

    In general I agree with @Sgoettschkes' suggestion to use query string arguments for this. If you need highly dynamic routing with flexible segments, you're doing something wrong in most cases. And query string is usually the better fit for those params.

提交回复
热议问题