• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 912
  • Last Modified:

zend MVC controller action corrupts CSS and image paths

Hi Experts

In the zend MVC framework, if I have a controller like www.foo.com.index and a controller www.foo.com/terms and want to neaten things up by moving terms to the index controller as an action ie. www.foo.com/index/terms the base url goes all wonky, so an image that used to be at www.foo.com/images/bar.jpg ends up at www.foo.com/index/images/bar.jpg

Thanks in advance
Gary Benade
Gary Benade
1 Solution

You can use the frontController to get your applications base url:

$front = Zend_Controller_Front::getInstance();
$baseUrl = $front->getBaseUrl();

Then use this with your image path:

<img src="<?php echo rtrim($baseUrl, '/');?>/images/bar.jpg" />

An alternative is to have a view_helper:

class My_View_Helper_BaseUrl

    public function BaseUrl($arg)
        $front = Zend_Controller_Front::getInstance();
        return rtrim($front->getBaseUrl(), '/') . $arg;

Then you can use inside your templates:

<img src="<?php echo $this->BaseUrl('/images/bar.jpg');?>" alt="Bar" />

Kind regards


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now