问题
Router::scope('/:club_slug', function ($routes) {
$routes->connect('/login', ['controller' => 'Users', 'action' => 'login']);
});
So when I'm trying access http://example.com/club-name/login, I'm being redirected to http://example.com/users/login with the flash message You have to login to access this area.
Auth loginAction is [controller => 'Users', 'action' => 'login'], since the custom route that I mentioned at beginning of the question is pointing to the path that is specified at loginAction I thought the route will know that I'm talking about the same thing but is not what is happening.
回答1:
Dynamic route elements are not being added/recognized automatically, you'll either have to persist them using either the persist option (applies only to that specific route):
Router::scope('/:club_slug', function ($routes) {
$routes->connect(
'/login',
['controller' => 'Users', 'action' => 'login'],
['persist' => ['club_slug']]
);
});
or URL filters (affects all routes that are using a club_slug element):
Router::addUrlFilter(function ($params, $request) {
if (isset($request->params['club_slug']) && !isset($params['club_slug'])) {
$params['club_slug'] = $request->params['club_slug'];
}
return $params;
});
or you have to pass the element to your login action manually (this would match the club_slug route regardless of the current URL):
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'club_slug' => 'club-slug' // wherever it may have come from
]
See also
- Cookbook > Routing > Creating Persistent URL Parameters
- API > Cake\Routing\RouteBuilder::connect()
来源:https://stackoverflow.com/questions/30239834/login-custom-route-being-rejected-by-auth