shin
shin

Reputation: 32721

Static content not displayed with Zend FW

I am trying to display a static content with Zend framework.

When I go to http://square.localhost/content/services, I get an error message.

Could anyone tell me how to fix this please?

Thanks in advance.

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  

application/modules/default/controllers/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);
      }
    }    
}

application/modules/default/views/scripts/static-content/services.phtml

some html
...
...

Error message

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',
)  

Upvotes: 2

Views: 991

Answers (3)

Sergey Butvin
Sergey Butvin

Reputation: 11

This construction "$this->view->getScriptPath()" return array. You may use like this "$this->view->getScriptPath()[0]"

Upvotes: 1

tawfekov
tawfekov

Reputation: 5122

let make some debugging together :

try to make your action like this , and update your question as we did last time

 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);
      }
    }    

update

this is weird ...
how come $page = about-us ?? and you are trying to browse http://square.localhost/content/services in your case it must be $page = services

and the second one false means that about-us.phtml page doesn't exist create an empty page with the name about-us.phtml and the error would go away

Upvotes: 0

Andy Baird
Andy Baird

Reputation: 6228

Debug your file_exists() line. Your view script file is in the wrong spot, or your looking in the wrong spot for the file. I would start by looking at the actual string thats get built from this:

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

Upvotes: 2

Related Questions