Zend FW不显示静态内容

时间:2010-12-28 20:46:13

标签: php zend-framework

我正在尝试使用Zend框架显示静态内容。

当我转到http://square.localhost/content/services时,收到错误消息。

有人能告诉我如何解决这个问题吗?

提前致谢。

的application.ini

....
....
resources.layout.layoutPath = APPLICATION_PATH "/layouts"
resources.layout.layout = "master"
resources.router.routes.home.route = /home
resources.router.routes.home.defaults.module = default           
resources.router.routes.home.defaults.controller = index
resources.router.routes.home.defaults.action = index  
resources.router.routes.static-content.route = /content/:page              
resources.router.routes.static-content.defaults.module = default           
resources.router.routes.static-content.defaults.controller = static-content
resources.router.routes.static-content.defaults.action = display  

应用/模块/默认/控制器/ StaticContentController.php

class StaticContentController extends Zend_Controller_Action
{
    public function init()
    {
    }

    // display static views
    public function displayAction()
    {
      $page = $this->getRequest()->getParam('page');
          if (file_exists($this->view->getScriptPath(null) . "/" . $this->getRequest()->getControllerName() . "/$page." . $this->viewSuffix)) {
        $this->render($page);
      } else {
        throw new Zend_Controller_Action_Exception('Page not found', 404);
      }
    }    
}

应用/模块/默认/视图/脚本/静态内容/ services.phtml

some html
...
...

错误消息

An error occurred
Page not found
Exception information:

Message: Page not found
Stack trace:

#0 /var/www/square/library/Zend/Controller/Action.php(513): StaticContentController->displayAction()
#1 /var/www/square/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('displayAction')
#2 /var/www/square/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#3 /var/www/square/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#4 /var/www/square/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#5 /var/www/square/public/index.php(26): Zend_Application->run()
#6 {main}  

Request Parameters:

array (
  'page' => 'services',
  'module' => 'default',
  'controller' => 'static-content',
  'action' => 'display',
)  

3 个答案:

答案 0 :(得分:2)

调试你的file_exists()行。您的视图脚本文件位于错误的位置,或者您正在寻找文件的错误位置。我首先看看从这个构建的实际字符串:

$this->view->getScriptPath(null) . "/" . $this->getRequest()->getControllerName() . "/$page." . $this->viewSuffix

答案 1 :(得分:1)

这种结构“$ this-> view-> getScriptPath()”返回数组。您可以像这样使用“$ this-> view-> getScriptPath()[0]”

答案 2 :(得分:0)

让我们一起调试:

尝试按照这样的方式进行操作,并像上次一样更新您的问题

 public function displayAction()
    {
      $page = $this->getRequest()->getParam('page');
      var_dump($page);
      var_dump(file_exists($this->view->getScriptPath(null) . "/" . $this->getRequest()->getControllerName() . "/$page." . $this->viewSuffix));
      die("the code will stop here");
          if (file_exists($this->view->getScriptPath(null) . "/" . $this->getRequest()->getControllerName() . "/$page." . $this->viewSuffix)) {
        $this->render($page);
      } else {
        throw new Zend_Controller_Action_Exception('Page not found', 404);
      }
    }    

更新

这很奇怪...... 怎么来$page = about-us ?? 并且您正在尝试浏览http://square.localhost/content/services 在您的情况下,它必须是$page = services

,第二个false表示about-us.phtml页面不存在 创建一个名为about-us.phtml的空白页面 并且错误会消失