zend MVC controller action corrupts CSS and image paths

Posted on 2008-11-15
Last Modified: 2013-11-10
Hi Experts

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

Thanks in advance
Question by:Gary Benade
    1 Comment
    LVL 49

    Accepted 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

    Easy Project Management (No User Manual Required)

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now