Search code examples
routesyii2yii-url-manager

How to correctly specify route that performs when standard one does not found in yii2?


Assuming we have a page with random seo friendly url like /some/page.html How to describe rules for urlManager in config that specifies condition in where if standard rote (e.g. SomeController with actionPage) does not exitsts than performs the specified default route (e.g. DefaultController / actionDefault)?

This rule overrides default route and forwards all requests to my specified route

'urlManager' => [
        ...
        'suffix' => '.html',
        'rules'=>[
            '<alias:[0-9a-zA-Z-_/]+>' => 'default/default',
        ]
]

In other words, algorithm should be as following:

  1. Check if the parsed url have matched with existing module/controller/action then handle this one
  2. Otherwice handle the request by the route predefined in config

Solution

  • I guessed the deal is not in routing. My problem was solved by extend urlManager class with override parseUrl method which checks for each request for existence of its url in DB. If is not present then returns parent method result. Thanks for attempt to help :)

    P.S. But if this url is coincident with standard routing scheme then overrides this one.