Search code examples
zend-frameworkcontrollerstylesheetzend-controller

Zend_Controller tries to execute my stylesheets as controller


I initialize my placeholders for my global layout within the Bootstrap.php as described here.

public function _initPlaceholders()
{
    $this->bootstrap('View');
    $view = $this->getResource('View');

    $view->doctype('XHTML11');

    $view->headTitle('Foo Bar Title')
         ->setSeparator(' :: ');

    $view->headMeta()->appendHttpEquiv(
        'content-type',
        'application/xhtml+xml; charset=UTF-8'
    );

    $view->headMeta()->appendName('robots', 'index,follow');

    $view->headLink()->appendStylesheet('/styles/styles.css', 'screen')
                     ->appendStylesheet('/styles/print.css', 'print');
}

The rendered HTML looks correct.

<title>Foo Bar Title</title>
<link href="/styles/styles.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/styles/print.css" media="print" rel="stylesheet" type="text/css" />
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
<meta name="robots" content="index,follow" />

But the CSS doesn't get loaded correctly because Zend_Controller thinks it's a controller or something. When I try to open the CSS files the following error occurs:

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)'

Any hints?

[update]

Ok, just added the following line to my .htaccess file and all works as expected now...

RewriteRule !.(js|ico|txt|gif|jpg|png|css|htc|swf|htm)$ index.php


Solution

  • A typical Zend project layout looks something like this:

    .
    |-- application
    |   |-- Bootstrap.php
    |   |-- configs
    |   |-- controllers
    |   |-- forms
    |   |-- layouts
    |   |-- models
    |   `-- views
    |-- library
    `-- public
        |-- images
        |   `-- favicon.ico
        |-- index.php
        |-- js
        |   `-- scripts.js
        `-- styles
            `-- style.css
    

    Does yours look similar? Specifically, do you have CSS and JavaScript files somewhere under the public folder (and not the application folder)? If so, can you review file permissions?

    Also, I recommend reviewing file permissions. If the CSS files aren't readable by the Apache process, then Apache won't be able to serve them.