【问题标题】:Cakephp auth tries to call non-existant controllerCakephp auth 尝试调用不存在的控制器
【发布时间】:2013-07-16 04:41:59
【问题描述】:

我的 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());
}

【问题讨论】:

  • 你用的是什么版本的 Cake?
  • AD7six:cakephp 版本 2.3.7

标签: authentication cakephp cakephp-2.3


【解决方案1】:

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

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

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

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

如需更清晰的想法,请转至cakephp.org link

【讨论】:

  • 都不能解决问题。
【解决方案2】:

添加父级::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。

【讨论】:

  • Hardik:你有没有读过上面那些人的帖子?还有其他的想法吗?
  • 你在apache中设置了文档根目录吗?
【解决方案3】:

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
    )
);

【讨论】:

    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 2012-04-18
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多