0

我的 auth 组件运行良好,只是它复制了我的 CakePHP 所在的文件夹。例如,我的整个 CakePHP 安装都在其中,localhost/rh/但是当登录重定向时,它会将用户发送到localhost/rh/rh/controller. 有什么想法吗?

应用控制器:

class AppController extends Controller {

  public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'index'),
            'authError' => "You are not authorized to access that page",
            'authorize' => array('Controller')
        )
    );

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

    public function beforeFilter() {
        $this->Auth->allow('index', 'view');
    }
}

用户控制器:

    class UsersController extends AppController {

//before filter to allow users to register
public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add'); // Letting users register themselves
}

//login action
public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}
//logout action
public function logout() {
    $this->redirect($this->Auth->logout());
}
4

3 回答 3

1

添加父::beforeFilter(); 到用户控制器中的 beforeFilter:

function beforeFilter() {
    $this->Auth->autoRedirect = false;
    parent::beforeFilter();
}

您还可以将重定向替换为用户控制器的登录方法:

$this->redirect($this->Auth->redirect());
Auth->redirect() returns

如需更清晰的想法,请访问cakephp.org 链接

于 2013-07-16T05:06:11.800 回答
0

添加父::beforeFilter(); 到用户控制器中的 beforeFilter:

function beforeFilter() 
{
 $this->Auth->autoRedirect = false;
 parent::beforeFilter();
}

您还可以将重定向替换为用户控制器的登录方法: dd parent::beforeFilter(); 到用户控制器中的 beforeFilter:

function beforeFilter() 
{
 $this->Auth->autoRedirect = false;
 parent::beforeFilter();
}

您还可以将重定向替换为用户控制器的登录方法:

$this->redirect($this->Auth->redirect());

Auth->redirect() 返回用户在被带到登录页面或 Auth->loginRedirect 之前登陆的 url。

于 2013-07-16T05:58:15.547 回答
0

Auth 组件中有一个名为“unauthorizedRedirect”的选项,这是当用户尝试访问不允许访问的页面时,Cake 将用户重定向到的 url。如果没有设置,Cake 会重定向到 /{app-directory},因此你的域名会出现在控制器应该在的位置。

在您的 AppController 中更改它

public $components = array(
    //your other components
    'Auth' => array(
        //your other options for Auth
        'unauthorizedRedirect' => 'url to redirect to' //can be in any form that Cake accepts urls in
    )
);
于 2014-07-31T12:29:53.110 回答