2011-09-01 51 views
0

假設有人打在URL http://mysite.com/comments/view/13CakePHP的控制器/行動的問題

但這absentaction沒有出現在評論控制器。

然後獲取正常的錯誤一樣,=>

Error: The action view is not defined in controller CommentsController 

Error: Create CommentsController::view() in file: app/controllers/comments_controller.php. 

<?php 
class CommentsController extends AppController { 

    var $name = 'Comments'; 


    function view() { 

    } 

} 
?> 

Notice: If you want to customize this error message, create app/views/errors/missing_action.ctp 

我想要做的是,如果有人點擊網址http://mysite.com/comments/view/13,如果動作不存在,那麼它會重定向到http://mysite.com/

我該如何處理未知/缺席的操作?

回答

2

這個技巧實際上工作得很好。 你需要創建一個文件app/app_error.php

<?php 


class AppError extends ErrorHandler { 

    public function error404($params){ 
     extract($params); 

     if(!isset($url)){ 
      $url = $action; 
     } 

     if(!isset($message)){ 
      $message =""; 
     } 

     if(!isset($base)){ 
      $base = ""; 
     } 

     $this->controller->redirect(array('controller'=>'pages','action'=>'home')); 
     //Or the page you want... 

    } 

} 


?> 

它是如何工作的?

它實際上覆蓋ErrorHandler中的error404()函數,並重定向用戶$this->controller->redict();

+0

查看[CakePHP API](http://api13.cakephp.org/class/error-handler)以獲取更多詳細信息 – Franquis

+0

我剛剛使用了這個=><?php class AppError擴展ErrorHandler函數missingAction($ params){this-> controller-> redirect(array('controller'=>'posts','action'=>'index')); \t \t} } ?>它正在工作。 – shibly

+0

很高興知道:) – Franquis

0

它說是正確的錯誤...

創建應用程序/視圖/錯誤/ missing_action.ctp

這就是你應該做的......

嘗試使用missing_action.ctp中的標題重定向到您想要的頁面去。

+0

無法理解,您對失蹤操作使用標頭意味着什麼? – shibly

1

注意錯誤消息的底部,它表示您可以通過創建app/views/errors/missing_action.ctp來定製它。因此,所有你需要做的就是創建一個.ctp文件,包括像這樣在它重定向:

<?php 
header('Location: http://mysite.com') ; 
?> 
+0

不,我不會創建該視圖和操作。其實你不能創造無限的觀點和行動。用戶可以使用任何未創建的操作來點擊網址。 – shibly

+1

@guru您誤解了:Brian的建議是,如果您使用重定向創建'missing_action.ctp'文件,您將獲得所需的功能。 – JJJ

+0

所以我應該創建空的視圖/錯誤/ view.ctp?我應該創建ErrorController嗎? – shibly

0

您可以自定義應用程序/視圖/錯誤/ missing_action.ctp也可以在應用程序關閉調試/ config/core.php

+0

提供的答案哪一行更改?價值是什麼? – shibly

+0

Configure :: write('debug',0); –