Cake php 2.x project redirecting to wrong url afte

2019-08-13 09:51发布

问题:

My Cake Php project is redirecting to the wrong url after I did the full implementation of ACL by following this tutorial -> http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html.

Problem -

Correct redirect -> localhost/appname/

Redirect after I implemented ACL -> localhost/appname/appname/

This is the redirect that occurs after login. The public pages (Login) work fine.

Below is Appcontroller code-

public $components = array( 'Acl',
'Auth' => array(
    'authorize' => array(
        'Actions' => array('actionPath' => 'controllers')
    )
),
'Session'
);
public $helpers = array('Html', 'Form', 'Session');
// only allow the login controllers only
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login');
}

Acos table screenshot Aros table screenshot Aros_Acos table screenshot
groups table

Routes.php

Router::connect('/dashboard', array('controller' => 'dashboards', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/', array('controller' => 'dashboards', 'action' => 'index'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';

The right url was opening when I used only "Auth" instead of the following.

'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),

BUT, then the ACL is not working.

回答1:

From the code you've pasted you don't seem to have configured a login redirect. I would expect your beforeFilter in your app controller to look more like this:

public function beforeFilter() {
    //Configure AuthComponent
    $this->Auth->loginAction = array(
      'controller' => 'users',
      'action' => 'login'
    );
    $this->Auth->logoutRedirect = array(
      'controller' => 'foo',
      'action' => 'bar'
    );
    $this->Auth->loginRedirect = array(
      'controller' => 'foo',
      'action' => 'bar'
    );
}

In your case you appear to want the logged in user to return to the home page so in this example your home page would be defined in app/config/routes.php as being the bar action of the foo controller.

In the controller where you define the login action (normally the users controller) you may also add a redirect, something like:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            // Redirect the user to home
            return $this->redirect($this->Auth->redirect(array('controller'=>'foo', 'action'=>'bar')));
        }
    }
}

You seem to be missing the ACL component in your AppController components array. Try updating it to:

public $components = array(
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        )
    ),
    'Session'
);