0
votes

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:

  • Application
  • Public
  • ....
  • index.php

    include 'public/index.php';

  • .htaccess

    SetEnv APPLICATION_ENV development RewriteEngine On RewriteRule .* index.php

In public_html/Project1/public I have:

  • CSS folder
  • images folder
  • js folder
  • .htaccess

    SetEnv APPLICATION_ENV development RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.$ - [NC,L] RewriteRule ^.$ index.php [NC,L]

  • index.php ( another one )

    // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(FILE) . '/../application'));

    // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

    // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), get_include_path(), )));

    /** Zend_Application */ require_once 'Zend/Application.php';

    // Create application, bootstrap, and run $application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini' );

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

    $application->bootstrap() ->run();

Thanks again...

Samuele

1
Post the difference in HTML here. Most likely you're using relative paths to access those resourcesJohnP
The problem is that the HTML is exactly the same...Samuele
Yes, this is most likely because the relative path will work from the index page and not the /index/action page because it is one level deeperJohnP
What is $config->baseHttp set to? And what does the output HTML look like for the CSS and img paths in the working and non-working pages?ChrisA
ChrisAnstey thanks for help me... Anyway the answer of Tim Fountain worked fine for me... :)Samuele

1 Answers

1
votes

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.