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
LVL 19
Gary BenadeAsked:
Who is Participating?
 
RoonaanCommented:
Hi,

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:

<?php
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

-r-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.