2013-07-16 19 views
0

我的驗證組件工作良好,但它複製了我的CakePHP所在的文件夾。例如,我的整個CakePHP安裝位於localhost/rh/中,但登錄重定向時它會將用戶發送到localhost/rh/rh/controller。有什麼想法嗎?Cakephp驗證嘗試調用不存在的控制器

的AppController:

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

UserController中:

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()); 
} 
+0

什麼版本的蛋糕您使用的是? – AD7six

+0

AD7six:cakephp版本2.3.7 –

回答

1

添加父:: beforeFilter();到beforeFilter在用戶控制器:

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

你也可以用這個代替重定向到用戶控制器的登錄方法:

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

爲更加清晰的想法只是去cakephp.org link

+0

既不能解決問題。 –

+0

將清除你的tmp緩存或檢查你的router.php – liyakat

0

添加父:: beforeFilter();到beforeFilter在用戶控制器:

您也可以使用此替換重定向到用戶控制器的登錄方法: DD父:: beforeFilter();到beforeFilter在用戶控制器:

您也可以使用此替換重定向到用戶控制器的登錄方法:

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

Auth->重定向()返回的網址,其中用戶在被帶到登錄頁面或Auth-> loginRedirect之前降落。

+0

Hardik:你甚至讀過你上面的人嗎?有沒有人有任何其他想法? –

+0

你有沒有在apache中設置文件根目錄? –

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