2013-05-15 31 views
0

我是cakephp框架的新角色。我不能調用控制器的功能。 控制器 -如何使用默認函數在cakephp框架中調用控制器的函數?

class PagesController extends AppController { 
    public $name = 'Pages'; 
    public $uses = array(); 

    public function display() { 
     $path = func_get_args(); 

     $count = count($path); 
     if (!$count) { 
      $this->redirect('/'); 
     } 
     $page = $subpage = $title_for_layout = null; 

     if (!empty($path[0])) { 
      $page = $path[0]; 
     } 
     if (!empty($path[1])) { 
      $subpage = $path[1]; 
     } 
     if (!empty($path[$count - 1])) { 
      $title_for_layout = Inflector::humanize($path[$count - 1]); 
     } 
     $this->set(compact('page', 'subpage', 'title_for_layout')); 
     $this->render(implode('/', $path)); 
    } 

    public function register() { 
     $this->set('fdf', 'chandan'); 
     $this->render('home1'); 
    } 
} 

但我調用顯示()。但我不是調用register()。我的routes.php文件喜歡 -

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); 
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); 

請幫幫我。如何從cakephp中查看調用控制器函數。 以及需要做些什麼設置?

+0

也許你應該閱讀一些關於MVC設計模式的基礎知識 –

+0

視圖不會調用控制器函數。調度員調用控制器,該控制器呈現我在codeigniter上工作的視圖 –

+0

。我們可以在這裏寫作,如 cbnvbxcbvnbxcbv

回答

1

有幾點我想提出,路由文件是用於定義自定義蛞蝓/ URL,看看你的第一個路由定義在這裏:

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); 

這是在說,「www.mysite.com/ 「應鏈接到控制器頁面,動作顯示並將第一個參數作爲主頁傳遞。

這可以通過做「www.mysite.com/pages/display/home」簡而言之 - 但使用「/」作爲一個路線是整潔的。一般的規律是「www.mysite.com/controller/action/param1/param2/etc ..」

所以按照這樣的邏輯,你會獲得新的操作方法就像這樣: 「www.mysite。 com/pages/register「

這就是說......使用MVC時,你應該遵循約定,如果你要創建一個註冊方法,你應該真正包含它在一個控制器中處理用戶帳戶,即「UsersController」 - 「www.mysite.com/users/register」

此外,你不應該真的需要你se $ this-> render(),除非必須在特殊條件下呈現單獨的視圖。總結一下,包含相關控制器(例如www.mysite.com/users/login和www.mysite.com/users/register)中的所有操作,從不直接指定$ this-> render,除非您確實需要以呈現除默認外的其他內容(/users/register.ctp將作爲www.mysite.com/users/register的默認值),並使用路由創建更整潔或自定義的網址。

我強烈建議您閱讀並按照博客教程掌握這些概念。

相關問題