Reputation: 6649
I would like have all routes except the api routes to navigate to the site/index route but all /api path to be executed to the respective modules. I have added the following route rules
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
//api module
'api/<controller:\w+>/<action:[\w\-]+>' => '<controller>/<action>',
'api/<controller:\w+>' => 'api/<controller>',
//all other paths
'<controller:[\w\-]+>/<action:[\w\-]+>' => 'site/index',
'<controller:[\w\-]+>/' => 'site/index',
],
],
The following works for 2 level url routes that is
/users/create
/users/view
But when i access routes with more than 2 paths like
/users/create/12
/admin/uom/create/new
The routes are not redirrected to site/index
What else do i need to add to ensure that all routes even with more than 3 paths are executed via site/index but those with api prefix are executed via controller/action
or /api/controller
.
What am i missing out?
Upvotes: 0
Views: 180
Reputation: 22174
There is no built-in support for such case. But you can create custom URL rule which will match any route:
class FallbackUrlRule extends Component implements UrlRuleInterface {
public $route = 'site/index';
public function parseRequest($manager, $request) {
return [$this->route, []];
}
public function createUrl($manager, $route, $params) {
return false;
}
}
And use it in your app config:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
//api module
'api/<controller:\w+>/<action:[\w\-]+>' => '<controller>/<action>',
'api/<controller:\w+>' => 'api/<controller>',
//all other paths
[
'class' => FallbackUrlRule::class,
'route' => 'site/index',
]
],
],
Note that it will simply ignore request path and catch all requests, including these that should throw 404 error. It will also not detect parameters in path (it will ignore 12
in /users/create/12
).
Upvotes: 0