how to sometimes switch baseUrl when calling url view helper

650 views Asked by At

i have a question about the best practice on urls in a zend application. My application will run in two different contexts (facebook and standalone). Normally my baseUrl is empty. But when run in Facebook (as canvas app) I set my baseUrl to be "apps.facebook.com/xxx"... but there are exceptions. Mostly I want the new baseUrl, but sometimes I do not. Currently I strip out the baseUrl, when I do not need it...

Thanks in advance

2

There are 2 answers

0
takeshin On

The best choice would be to subclass the BaseUrl view helper and use the extended one when you need the custom value.

2
Marcin On

You can modify baseUrl in e.g. your view as follows:

<?php 
    // save current base
    $oldBase = $this->getHelper('baseUrl')->getBaseUrl();

    // set a new one for a short time
    echo $this->getHelper('baseUrl')->setBaseUrl('new/base/')->baseUrl('link');

    // restore the original base
    $this->getHelper('baseUrl')->setBaseUrl($oldBase);
?>

It is important to remember to save and restore the original base for the baseUrl. Otherwise, all layout may not work properly as helper 'remember' their state and you don't want to change the base for system wide. Hope this will be helpful.