2013-02-20 41 views
2

您好,我的代碼在CakePHP框架中不起作用,它顯示一條錯誤消息。Cakephp通過參數

網址:

http://domainname.com/About/param1/param2 

代碼:

class AboutController extends AppController { 

    public $name = 'About'; 
    public $helpers = array('Html'); 

    public function index($arg1, $arg2) 
    { 
     print_r($this->request->params['pass']); 
     $this->set('title_for_layout','Sarasavi Bookshop - About Sarasavi Bookshop'); 
     $this->set('nameforfiles','about'); 
    } 
} 

錯誤消息:

Missing Method in AboutController  
Error: The action param1 is not defined in controller AboutController 
Error: Create AboutController::param1() in file: app\Controller\AboutController.php. 

<?php 
class AboutController extends AppController { 


    public function param1() { 

    } 

} 

Notice: If you want to customize this error message, create app\View\Errors\missing_action.ctp 

創建FUNC後我可以得到param2,但是我需要得到param1param2兩者,在index函數中沒有創建另一個動作。

請幫助我,謝謝

回答

2

如果你去http://domainname.com/About/index/param1/param2

如果你不想在URL中index做,因爲我認爲你不這樣做你原來的代碼將工作,那麼你需要定義一條路線。

添加到您的路線:

Router::connect(
    '/About/*', 
    array('controller' => 'About', 'action' => 'index') 
); 

自動不指定動作,但這樣做將請求路由有PARAMS去你index行動。儘管如此,您仍然需要爲任何新的About操作添加一條路由來停止對它們的請求,以使其不會進入索引。

+1

是它的工作,非常感謝你!馬特 – iswan 2013-02-20 10:36:16

2

你可能訪問此網址沒有這樣的規定動作名稱(指數):

/about/param1/param2

這將無法正常工作,蛋糕希望看到控制器名之後的動作名稱。在這種情況下,param1被視爲操作名稱。 你可以用URL

/about/index/param1/param2

訪問你的動作要克服這種情況,作出新的規則,以你的路由器:

Router::connect(
    '/about/:param1/:param2', 
    array('controller' => 'about', 'action' => 'index'), 
    array('param1', 'param2') 
);