【问题标题】:cakephp pages_controller extends instead of overwrites, is that possible?cakephp pages_controller 扩展而不是覆盖,这可能吗?
【发布时间】:2011-11-03 14:44:16
【问题描述】:

我需要在“/cake/libs/controller/pages_controller.php”中添加新功能,但我不想直接更改它,因为它是核心 cakephp 的一部分,因此我所做的就是复制“pages_controller.php”。 php” 进入“app/controllers/”,然后我添加了新功能,但出现了一些错误,例如“控制器 PagesController 中未定义动作显示”。

注意事项:

  • /cake/libs/controller/pages_controller.php确实有函数display()
  • /app/controllers/pages_controller.php 没有 display()

有什么问题?为什么我会收到这个错误?

这是 /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'));

【问题讨论】:

  • 显示 /app/controllers/pages_controller.php

标签: cakephp


【解决方案1】:

当你创建时

/app/controllers/pages_controller.php

覆盖

/cake/libs/controller/pages_controller.php

所以display() 需要在您的 PagesController 中,因为您正在路由到它。您可能希望将display() 保留为复制状态并编写类似

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

【讨论】:

    【解决方案2】:

    由于app/config/routes.php 中的这一行,您的应用可能正在寻找display() 操作:

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

    您正在尝试访问页面控制器中的操作,但 cake 正在寻找 display() 操作。

    编辑:看到您的PagesController 后,唯一可能的错误原因是routes.php 文件

    【讨论】:

    • 是的,我有这个:Router::connect('home/', array('controller' => 'pages', 'action' => 'display', 'home')); Router::connect('/successfully', array('controller' => 'pages', 'action' => 'display_no_layout', 'successfully'));
    • @lito 你有Router::connect('/pages/* ...' 吗?
    猜你喜欢
    • 1970-01-01
    • 2019-04-17
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    相关资源
    最近更新 更多