Some people have reported issues with accessing, setting, or getting the right value from baseUrl() in a view script. But I’m wondering why it is necessary to use it at all, at least in a situation like mine where the ZF application is on a virtual private host (Amazon EC2) where I have full control of the directory structure and apache rewrite rules, as well as routes.
I know, for example, that in the filesystem foo.jpg lives in public/images/foo.jpg, and that the application’s mod_rewrite will direct all requests to public – so in my view scripts it’s a lot simpler/clearer and more efficient to write something like
<img src="/images/foo.jpg" />
instead of
<img src="<?php echo $this->baseUrl();?>/images/foo.jpg" />
What sort of future-proofing robustness or other benefit does the use of baseUrl() really provide? So far I haven’t used it at all, and had no problem. But I’ve inherited some code that uses it, and my inclination is to strip out those uses whenever I’m editing a view script that contains them. Would I regret that later?
Used this way, it’s not really useful, but on the other hand, using it this way
might prove to be useful in the future since you can add logic before printing the URL. Imagine that in a few years your website grows way more than you expected and you have to move all your static content to a
Content delivery network (CDN)you will have to manually (or with search and replace) correct all your images/css/js instances URLs. With thebaseUrl()(or as name itassetUrl()) you would just have to add your CDN’s url and it will be fixed everywhere in your application.EDIT
I found a use for the
baseUrl()in the code you inherited :It would allow you to add a common URL part to all of your links and references, in the case that your site is not at the root of the domain
In your config file you would just have to add
for it to work, and all of your links would be prepended with that part