Today i was working with the Zend Framework. And needed the baseUrl to set the correct path for stylesheets and external javascript files. To include stylesheets and javascript files there are methods available in ZF. In your controller you can set:
$this->view->headLink()->appendStylesheet('/css/tester.css');
In your view or layout you can then call
$this->headLink();
This works great. But the current development structure lacks some good rewrite rules. And having the baseUrl is always a good thing. So i did some searching on Google. To figure out if somebody already has a solution for this problem. After a few minutes i found a post written by Yusak Setiawan from zendindonesia (which is defaced by the way) explaining how to add a placeholder in the bootstrap file. Now this is a good solution. But i like to keep my bootstrap file as clean as possible. So i decided to add a Helper that would do the trick.
Create a file called ‘BaseUrl.php’ inside the Helper directory. And fill it with the following code.
class Zend_View_Helper_BaseUrl
{
function baseUrl()
{
$base_url = substr($_SERVER['PHP_SELF'], 0, -9);
return $base_url;
}
}
After that it’s extremly easy to get the baseUrl in your views, layouts or controllers.
Inside the view or layout you can call:
$this->baseUrl();
And inside the controller you can call it like this.
$this->view->baseUrl();
Tudor Barbu wrote a great article on this subject.



Thijs Lensselink is a PHP developer, consultant and all out open source enthusiast.
He has over 12+ years of experience in building and maintaining web applications mostly
on linux/Unix/BSD platforms. Besides a full time job he does freelance work with his ...