Samuele
Samuele

Reputation: 510

Zend Framework css image just url with cotroller but not with the action

I'm learning Zend Framework... having this problem...

If I go to the URL www.mydomain.com/Index will work FINE but I go to the URL www.mydomain.com/Index/index the CSS and IMAGE will NOT WORK...

OR for example:

If I go to the URL www.mydomain.com/Faq will work FINE but I go to the URL www.mydomain.com/Faq/test the CSS and IMAGE will NOT WORK...

I don't understand why it work only if you specify the Controller and not Action...

In my layout.phtml I have:

<?php
$this->headLink()->appendStylesheet(BASE_URL . 'css/style.css');
$this->headLink()->appendStylesheet(BASE_URL . '/css/header.css');
    $this->headLink()->appendStylesheet(BASE_URL . 'css/footer.css');
echo $this->headLink();
?>

And in public/index.php I have:

$config = new Zend_Config_Ini(
    APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
$baseUrl = $config->baseHttp;
define('BASE_URL', $baseUrl);

And for example an image in layout.phtml:

<img src="<?= BASE_URL ?>immagini/footer_info.png" alt="info" />

What's wrong?

Maybe the problem is the .htaccess or those stuff... So I tell you more...

My hosting have the public root: public_html

Inside public_html I have different project folder. Example: Project1 and Project2 ( both with Zend Framework ).

In public_html/Project1 folder I have:

In public_html/Project1/public I have:

Thanks again...

Samuele

Upvotes: 0

Views: 1868

Answers (1)

Tim Fountain
Tim Fountain

Reputation: 33148

I'm guessing BASE_URL is actually empty, so your issue is that you're using relative paths for your stylesheets and images, which won't work in subfolders. For example, on www.mydomain.com/Faq, a stylesheet include for css/style.css will resolve to www.mydomain.com/css/style.css which presumably is correct. On www.mydomain.com/Faq/test it will resolve to www.domain.com/Faq/css/style.css which is not correct.

The solution is to always use absolute paths:

$this->headLink()->appendStylesheet(BASE_URL . '/css/style.css');
$this->headLink()->appendStylesheet(BASE_URL . '/css/header.css');
$this->headLink()->appendStylesheet(BASE_URL . '/css/footer.css');

(note the slash before css). Then, if you are setting base URL, make sure it does not include a trailing slash, e.g. http://www.mydomain.com. Everything should work correctly then.

Upvotes: 1

Related Questions