Login custom route being rejected by Auth

自闭症网瘾萝莉.ら 提交于 2019-12-12 06:15:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!