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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.