Login custom route being rejected by Auth

2019-09-04 08:27发布

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条回答
你好瞎i
2楼-- · 2019-09-04 09:16

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

查看更多
登录 后发表回答