Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route all requests, not matching other routes, to single action

Tags:

yii2

I used catchAll route to my config

'catchAll' => ['site/page']

and it works fine.

But all request are going to this action, even that one that have a separate controller/action.

How can I map the routing to match the controller/action at first, and if corresponding controller/action doesn't not exists, then fallback to catch all route?

like image 496
beardeddev Avatar asked Oct 28 '14 09:10

beardeddev


1 Answers

This config does what you want:

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        'contact' => 'site/contact',
        '/' => 'page/view',
------> HERE
        '/<url:[a-zA-Z0-9-]+>' => 'site/page',
    ],
],

You should probably modify the rule to catch more chars. I used this, because I know my routes.

The catchAllshould be used when you want to put the website into maintenance mode. The error catch can also be used in this case.

like image 163
Mihai P. Avatar answered Sep 22 '22 20:09

Mihai P.