2011-11-03 44 views
0

我需要將新功能添加到「/cake/libs/controller/pages_controller.php」,但我不想直接更改它,因爲它是核心的cakephp的一部分,因此我所做的就是複製「pages_controller.php 「到」app/controllers /「中,然後我添加了新的功能,但是我收到了一些錯誤,如」操作顯示沒有在控制器PagesController中定義「。cakephp pages_controller擴展而不是覆蓋,這可能嗎?

注:

  • /cake/libs/controller/pages_controller.php確實有功能顯示()
  • /app/controllers/pages_controller.php沒有顯示()

問題是什麼?爲什麼我得到那個錯誤?

這是/app/controllers/pages_controller.php:

<?php 
class PagesController extends AppController { 
    var $name = 'Pages'; 
    var $helpers = array('Html', 'Session'); 
    var $uses = array(); 

    function display_no_layout() { 
     $this->autoLayout = false; // new line 
     $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)); 
    } 
} 

我/app/config/routers.php:

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

顯示/app/controllers/pages_controller.php – cetver

回答

4

當您創建

/app/controllers/pages_controller.php 

覆蓋它

/cake/libs/controller/pages_controller.php 

所以需要display()因爲你正在路由到它,所以你需要在你的PagesController中。你可能想保持display()如複製和喜歡寫東西

function display_no_layout() { 
    $this->autoLayout = false; 
    $this->display(); 
} 
+0

太棒了!謝謝,你打我的問題的目標!) – lito

0

這有可能是您的應用程序正在尋找display()因此行動app/config/routes.php

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

你是試圖訪問頁面控制器中的操作,但蛋糕正在尋找display()操作。

編輯:看到你PagesController後,唯一可能的原因錯誤是routes.php文件

+0

是的,我有這樣的: 路由器連接::(「home /」中,陣列('控制器'=>'pages','action'=>'display','home')); Router :: connect('/ successful',array('controller'=>'pages','action'=>'display_no_layout','successfully')); – lito

+0

@lito你有行'Router :: connect('/ pages/* ...''? – Nasreddine