Code snippets for symfony 1.x


baseurl_for with an external application

Here is a usefull helper to calculate url for another application :

function baseurl_for($application, $absolute = false)
    $url = $absolute ? 'http://' . $_SERVER["HTTP_HOST"] : '';
    $url .= '/' . $application . (SF_ENVIRONMENT != 'prod' ? '_' . SF_ENVIRONMENT : '') . '.php/';
    return $url;

in the action :

<?php echo link_to('Whatever', baseurl_for('application', true) . 'module/action') ?>

I really feel like this feature is missing, hope this could added soon ;) ... example corrected (but sadly not so usefull) according to francois's comment.

by brikou on 2006-11-24, tagged application  base  helper  url 

Comments on this snippet

gravatar icon
#1 Francois Zaninotto on 2006-11-24 at 09:17

There is something I don't understand. How can the url_for() work for another application? One application doesn't have knowledge of another application's routing rules. So the only way you can link to another application is by specifying the external url (not the internal uri). For this reason, your example looks wrong.

Besides, your helper works only for one type of configuration. How about applications which front controllers are in subdirectories?

gravatar icon
#2 Olivier Verdier on 2006-11-24 at 09:59

Yes, people don't seem to understand that if you need links from one application to another then you should merge your two applications in one. There is no other way. As i see it, this snippet is a trick, that will not work in most of the cases.

My greatest hope is that it be possible in symfony 1.0 to split an application in several sub-applications with sub-application settings.

gravatar icon
#3 Jordi Backx on 2006-11-30 at 12:10

I have specified baseurl and favicon vars in my app.yml:

all: head: base: favicon: /images/favicon.ico

In my global template where the head section is built, I put: <?php echo tag('base', array('href' => sfConfig::get('app_head_base')))."\n" ?>

This way you can leave al your links alone. Only problem the baseurl is hardcoded ... it would be perfect if it is genereated inside the yml or something. Deployment is tricky this way.

gravatar icon
#4 Mark Fu on 2010-11-08 at 06:13

I encountered a problem: Notice: Use of undefined constant SF_ENVIRONMENT - assumed 'SF_ENVIRONMENT' in lib/helper/BaseUrlHelper.php on line 5

Need to use sfConfig::get('sf_environment') instead.