Auth repeats controller in URL

2019-08-05 09:05发布

I am setting for the first time the Auth component on my site, and everything seems to work fine except when I try to access a restricted page. Instead of being redirected to http://localhost/MySite/users/login, I get redirected to http://localhost/MySite/users/users/login, the controller name is repeated on the url. How can this issue be fixed?

I am using CakePhp 2.4.4

AppController

class AppController extends Controller {
public $components = array('DebugKit.Toolbar',
                            'Session','Auth' => array(
                                        'loginRedirect'=> array(
                                            'controller' => 'admins',
                                            'action' => 'admin_index'
                                        ),
                                        'logoutRedirect' => array(
                                            'controller' => 'users',
                                            'action' => 'login' 
                                        ),
                                        'loginAction' => array(
                                            'controller' => 'users',
                                            'action' => 'login',
                                            'plugin' => 'users'
                                        ),
                                        'authError' => 'Não tem permissão para aceder a esta área. Por favor faça login.',
                                        'authenticate' => array(
                                            'Form' => array(
                                                'fields' => array('username' => 'username', 'password' => 'password'
                                                    ),
                                                'userModel' => 'User'
                                            )
                                        ),
                                        'authorize' =>array('Controller'
                                        )
                                    )
                        );

public function beforeFilter(){
    $this->Auth->allow('index','ShowImages','ShowShowbill','ShowVideos','ShowContactUs','contact','login','DisplayMusic','DisplayEntertainment','DisplayPromotion','DisplayStaff','DisplayEquipments');

}

1条回答
爷、活的狠高调
2楼-- · 2019-08-05 09:43

In Auth component you need to add 'unauthorizedRedirect' otherwise Cake tries to redirect to /{app-directory} (this was giving me a headache yesterday).

public $components = array(
    //your other components
    'Auth' => array(
        //your other options for Auth
        'unauthorizedRedirect' => '/home'
    )
);

This would direct any user trying to access a page they shouldn't be allowed on to 'yourDomain/home'.

查看更多
登录 后发表回答