CakePHP Auth loginRedirect error/always redirect to 'users/login' whereas i put different controller

梦想与她 提交于 2019-12-08 09:23:56

问题


CakePHP Auth loginRedirect error/always redirect to 'users/login' whereas i put different controller. I mean, when i open the forbidden page(not allowed/require login)

$this->Auth->allow('index', 'profile', 'view', 'register');

it must redirect to "players/index". I put the loginRedirect to "players",

'loginRedirect' => array('controller' => 'Players', 'action' => 'index'),

but it doesn't work. It always redirect to "users/login" not "players/index" whereas i write "'loginRedirect' => array('controller' => 'Players', 'action' => 'index')".

this is my code:

class AppController extends Controller {
public $components = array(
    'Session',
    'Auth'=>array(
        'loginRedirect' => array('controller' => 'Players', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'Players', 'action' => 'index'),
        'authError'=>"Anda tidak dapat mengakses halaman.",
        'authorize'=>array('Controller')
    )
);

public function isAuthorized($user) {
    return true;
}

public function beforeFilter() {
    $this->Auth->allow('index', 'profile', 'view', 'register');
    $this->set('logged_in', $this->Auth->loggedIn());
    $this->set('current_user', $this->Auth->user());
}}

My table's name : players

why the result's always redirect to "users/login" not "players/" or "players/index"? please tell me why this happens and how i can solve it. Thank you!


回答1:


Have you tried to lowercase controller name ? Players => players

'loginRedirect' => array('controller' => 'players', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'players', 'action' => 'index'),



回答2:


I was stuck with the same issue for hours. Set the login action in the beforeFilter of your AppController as following:

$this->Auth->loginAction = array('controller'=>'yourcontollername', 'action'=>'login');

I followed the video youtube.com/watch?v=zvwQGZ1BxdM, see the first reply.




回答3:


very interesting, i come across a similar problem - after login redirect to the default home page. I have tried all above methods, but none of them could solve the issue.

finally, i found out that login form did not build properly which action and controller were not set. therefore the html form pointed to '/', when posted. However, the system still managed to login to right accounts, but none of redirect function worked in this situation.

It might be something you need to look into.

good luck.




回答4:


The answer lies in the beforeFilter function in AppController.php. You must set allowances for the Auth object.

public function beforeFilter() {
    // put in the functions that point to the views you want to be able to see 
    // without logging in. This works for all controllers so be careful for naming
    // functions the same thing. (all index pages are viewable in this example)
    $this->Auth->allow('index', 'thePageIWantToSee', 'userAdd', 'landingPage');
}



回答5:


Simply use the login() function in your Users/Players Controller. With the if cause you can redirect to an diffrent page.

public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect('/account'); //$this->redirect($this->Auth->redirectUrl());
            }                
            return $this->redirect( ['controller' =>'pages', 'action' => 'login-fail']);
        }
    }

Example used in CakePHP 3.2



来源:https://stackoverflow.com/questions/12444274/cakephp-auth-loginredirect-error-always-redirect-to-users-login-whereas-i-put

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