【问题标题】:How do I add a page to this zend project如何将页面添加到此 zend 项目
【发布时间】:2013-04-18 06:54:28
【问题描述】:

所以我被分配到这个使用 Zend Framework 1.12 的项目。我正在尝试向项目添加一个页面,但我似乎在某处遗漏了一些东西。目录结构与我见过的任何示例 Zend 项目都有点不同(没有引导目录)。

-应用

---控制器

-----模块

---------------无页面

------------ IndexController.php

---型号

----一堆db文件

---浏览量

----模板(智能 tpl 文件)

---------------无页面

------------ nopage.tpl

---后端

----router.php

这是我的 router.php 代码

$controller -> addControllerDirectory(ROOT . 'application/controller/nopage', 'nopage');
$router = $controller -> getRouter();

$nopage = new Zend_Controller_Router_Route_Regex(
'nopage.html',
array('module' => 'nopage', 'controller' => 'index', 'action' => 'index')
);

$router -> addRoute('nopage', $nopage);

这是我的 nopage IndexController.php 的 IndexController 代码

<?php

/** Zend_Controller_Action */
Zend_Loader::loadClass('System_Controller_Action');


class Nopage_IndexController extends System_Controller_Action 
{   

public function indexAction() {

            $this -> smarty -> assign('PageBody', 'nopage/404.tpl');
            $this -> smarty -> assign('Title', 'PetIdMe - 404');
            $this -> smarty -> display('layouts/main.tpl');
    } 
}

我得到这个错误:

指定的控制器无效(索引)

我的代码似乎遵循相同的结构和所有其他路线所做的一切,我已经搜索和搜索无济于事。 SO的知识渊博和慷慨的人在这里有什么想法吗?如果您需要更多信息,我很乐意提供。非常感谢您对此的任何见解。

从路由器编辑一些额外的代码

$controller -> addControllerDirectory(ROOT . 'application/controllers/lostandfound',      'lostandfound');

$controller -> addControllerDirectory(ROOT . 'application/controllers/search', 'search');

$controller -> addControllerDirectory(ROOT . 'application/controllers/orderstatus', 'orderstatus');

$controller -> addControllerDirectory(ROOT . 'application/controllers/myaccount', 'myaccount');


$controller -> addControllerDirectory(ROOT . 'application/controllers/giftcard', 'giftcard');

$controller -> addControllerDirectory(ROOT . 'application/controller/nopage', 'nopage');



$router = $controller -> getRouter();


$nopage = new Zend_Controller_Router_Route(
'nopage.html',
array('module' => 'nopage', 'controller' => 'index', 'action' => 'index')
);

$router -> addRoute('nopage', $nopage);
//****************** Gift Card ************************************************************

$giftcard = new Zend_Controller_Router_Route_Regex(
'giftcard.html',
array('module' => 'giftcard', 'controller' => 'index', 'action' => 'index')
);
$router -> addRoute('giftcard', $giftcard);

$GiftCardsPages = new Zend_Controller_Router_Route_Regex(
'admin/gift/page/(\d*)',
array('module'=>'admin', 'controller'=>'gift', 'action'=>'index'),
array(1  =>'page')
);
$router -> addRoute('GiftCardsPages', $GiftCardsPages);

//****************** SEARCH ***************************************************************
$topsearchserult = new Zend_Controller_Router_Route_Regex(
'topsearchserult.html',
array('module'=>'search', 'controller'=>'index', 'action'=>'search'),
array(1  =>'page')
);
$router -> addRoute('topsearchserult', $topsearchserult);

//****************** MY ACCOUNT *********************************************************

$myaccount = new Zend_Controller_Router_Route_Regex(
'myaccount.html',
array('module' => 'myaccount', 'controller' => 'index', 'action' => 'index')
);
$router -> addRoute('myaccount', $myaccount);

还有一些来自其他 IndexController 页面:

Zend_Loader::loadClass('System_Controller_Action');

class News_IndexController extends System_Controller_Action {

public function init() {
    parent::init();
}

public function viewAction() {
    $new = $this -> News -> getNewById($this->_getParam('new_id'));
    $this->smarty->assign('new', $new);
    $this->smarty->assign('Title', $new['new_title']);
    $this->smarty->assign('PageBody', 'news/show_item.tpl');
    $this->smarty->display('layouts/main.tpl');
}

public function indexAction() {

    $page = $this->_hasParam('page')?((int)$this->_getParam('page')-1):0; 
    $items = $this->News->getNewsForPage($page);

    $this->smarty->assign('items', $items);
    $this->smarty->assign('Title', 'News items');
    $this->smarty->assign('page_num', $page+1);
    $this->smarty->assign('page_count', $this->News->getPagesCount());
    $this->smarty->assign('PageBody', 'news/index.tpl');
    $this->smarty->display('layouts/main.tpl');
}

还有一个

Zend_Loader::loadClass('System_Controller_Action');
include_once ROOT . 'application/models/GiftCardsDb.php';
class GiftCard_IndexController extends System_Controller_Action 
{
private $giftcard;
 public function init() {
    $this->giftcard = new GiftCardDb();
    parent::init();
}

public function indexAction() {
    if($this->_hasParam('product_id')){
        $this -> smarty -> assign('giftcard_text', $this -> Content ->     getPageByLink('giftcard.html'));
        $this -> smarty -> assign('giftcard_agreement_text', $this -> Content -> getPageByLink('giftcard_agreement.html'));
        $this -> smarty -> assign('PageBody', 'giftcard/index.tpl');
        $this -> smarty -> assign('product_id', $this->_getParam('product_id'));
        $this -> smarty -> assign('Title', 'Pet Id Me - Gift Card');
        $this -> smarty -> display('layouts/main.tpl');
    } else {
        $this->_redirect("/");
    }
    }

【问题讨论】:

  • 在你的 $controller -> addControllerDirectory(ROOT . 'application/controller/nopage', 'nopage');你能回应 ROOT 的 VALUE 并告诉我你得到了什么吗?
  • 根路径正确,还有大约30个其他'$controller -> addControllerDirectory(ROOT .'application/controller/XXX', 'XXX');'在工作正常的路由器中。
  • 您能否粘贴任何其他示例,您如何为任何其他页面设置路线。你一定没看错吧?
  • 已编辑以显示更多示例。
  • 为什么你使用新的 Zend_Controller_Router_Route 而其他地方却是新的 Zend_Controller_Router_Route_Regex?

标签: php zend-framework


【解决方案1】:

你添加zend框架额外的页面给出了一些步骤

1.zend 是 MVC 模式

2.创建视图文件名和控制器文件名同名

示例:view->save.phtml 控制器:保存操作

3.你包括数据库连接实现控制器(或)调用db方法 参考以下链接 http://www.phpeveryday.com/articles/Zend-Framework-Basic-Tutorial-P840.html

【讨论】:

    【解决方案2】:

    我认为解决方案是添加这一行

    Zend_Loader::loadClass('System_Controller_Action');
    

    在 IndexController 的开头。即

    Zend_Loader::loadClass('System_Controller_Action');
    
    class Nopage_IndexController extends System_Controller_Action 
    {
    

    【讨论】:

    • 它在那里,只是在示例中格式化,没有显示为代码。
    • 那么你可以尝试清除存储文件夹中的缓存文件夹吗(我希望)可能是当你向路由添加新配置时,你需要你的路由不要从缓存中选择。不确定但试试看?
    • 我正在研究如何做到这一点,如果我更改控制器: $nopage = new Zend_Controller_Router_Route_Regex( 'nopage.html', array('module' => 'nopage', 'controller' => '索引', '动作' => '索引') ); $router -> addRoute('nopage', $nopage);到别的东西它会改变错误。还有什么可能导致这种情况?
    • 指定的控制器无效(插入我将控制器数组值更改到此处的任何内容)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 2014-10-12
    相关资源
    最近更新 更多