Tuesday, November 3, 2009

Setting Sitewide URL Prefix in Zend

So recently I came across the need to put a site into a test folder. Now I have done this in the past and it is very easy using a global domain name variable to change your domain from http://something.com/ to http://something.com/test/. If you have you own routing class you can just tell it to ignore the first parameter in the URI.

But here is the problem: I was trying to do this with the Zend routing class on a site that was already configured at setup with all its routing rules. Originally I tried just prepending 'test/' to all of the existing rules but that caused more complications than I had time to fix. I tried Googling a quick fix for this but could find nothing other than how to reroute one rule to another or forward to another controller. While it was useful information and I may need it in the future it was unrelated to my issue.

What I ended up doing was digging through the Zend_Controller_Front class to see what I could find. And voila I came across a class variable _baseUrl. Looking through the class methods I quickly found the setBaseUrl() method. After smacking myself on the head for not thinking of this earlier I then set called the already set controller class to call
Zend_Controller_Front::setBaseUrl('/test/');
And like magic the Zend controller started recognizing all my URL's that were prepended with '/test/'.

Hope this saves someone else a couple hours of searching.

No comments: