CakePHP Auth loginRedirect error/always redirect t

2019-06-09 14:11发布

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!

5条回答
戒情不戒烟
2楼-- · 2019-06-09 14:21

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

查看更多
放我归山
3楼-- · 2019-06-09 14:22

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');
}
查看更多
劳资没心,怎么记你
4楼-- · 2019-06-09 14:35

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.

查看更多
别忘想泡老子
5楼-- · 2019-06-09 14:36

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.

查看更多
一夜七次
6楼-- · 2019-06-09 14:43

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

'loginRedirect' => array('controller' => 'players', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'players', 'action' => 'index'),
查看更多
登录 后发表回答