All pastes #1266639 Raw Edit

CrossAppUrlHelper for sf 1.1

public text v1 · immutable
#1266639 ·published 2008-11-25 15:01 UTC
rendered paste body
<?php
/**
 * @author Olivier Mansour
 * @source http://www.glagla.org/weblog/2008/07/24/liens-inter-applications-avec-symfony-11/
 */

/**
 * return an url for a given symfony application and an internal url
 * work with symfony 1.1
 * freely inspired from sfWebControlleur code
 *
 * @author Olivier Mansour
 *
 * @param string $appname
 * @param string $url
 * @param boolean $absolute
 * @param string $env
 * @param boolean $debug
 * @return string
 */
function cross_app_url_for($appname, $url, $absolute = 'false', $env = null, $debug = 'false')
{
    // save the current configuration
    $currentConfiguration = sfContext::getInstance()->getConfiguration();

    if (sfConfig::get('sf_no_script_name'))
    {
        // wont work
        throw new sfException(__FUNCTION__.' : the cross app link helper will not work with sf_no_script_name to true');
    }

    // get the environment
    if (is_null($env))
    {
        $env = sfContext::getInstance()->getConfiguration()->getEnvironment();
    }

    // context creation
    if (!sfContext::hasInstance($appname))
    {
        $c = ProjectConfiguration::getApplicationConfiguration($appname, $env, $debug);
        sfContext::createInstance($c, $appname);
    }

    $request = sfContext::getInstance($appname)->getRequest();
    $url_root = $request->getRelativeUrlRoot();
    if ($absolute)
    {
        $url_root = 'http'.($request->isSecure() ? 's' : '').'://'.$request->getHost().$url_root;
    }

    //scriptname
    $scriptname = '';
    if (($env != 'prod') and ($env))
    {
        $env_suf = '_'.$env;
    }
    else
    {
        $env_suf = '';
    }
    if (!file_exists(sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.$appname.$env_suf.'.php'))
    {
        //test with index ?
        if (file_exists(sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'index'.$env_suf.'.php'))
            $scriptname = 'index'.$env_suf.'.php';
        else
        throw new sfException(__FUNCTION__.' : can\'t find a script name for appname : '.$appname.' and env : '.$env);
    }
    else
    {
        $scriptname = $appname.$env_suf.'.php';
    }

    $fragment = '';
    // strip fragment
    if (false !== ($pos = strpos($url, '#')))
    {
        $fragment = substr($url, $pos + 1);
        $url = substr($url, 0, $pos);
    }

    // generate url
    $parameters = sfContext::getInstance($appname)->getRouting()->parse($url);

    if (sfConfig::get('sf_url_format') == 'PATH')
    {
        // use PATH format
        $divider = '/';
        $equals  = '/';
        $querydiv = '/';
    }
    else
    {
        // use GET format
        $divider = ini_get('arg_separator.output');
        $equals  = '=';
        $querydiv = '?';
    }

    $web_url = $url_root.$querydiv.$scriptname.sfContext::getInstance($appname)->getRouting()->generate(null, $parameters, $querydiv, $divider, $equals);

    if ($fragment)
    {
        $web_url .= '#'.$fragment;
    }

    // restore the default / current configuration
    sfContext::switchTo($currentConfiguration->getApplication());

    return $web_url;
}