Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "url"

Generating Symfony URLs in other projects and subdirectories

Integrating Phorum into a Symfony project at http://www.example.com/forum, I wanted to use my existing Symfony URLs in my Phorum templates:

// This path should be the path to your config/ProjectConfiguration.class.php file
// in your Symfony project
require_once(dirname(__FILE__).'/../../../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', sfConfig::get("sf_environment"), true);
$instance = sfContext::createInstance($configuration);
 
$routing = $instance->getRouting();
 
// Our URLs may have a prefix (eg /forum/index.php).  Remove this
$routingOptions = $routing->getOptions();
$urlPrefix = (array_key_exists("prefix", $routingOptions["context"]) ? $routingOptions["context"]["prefix"] : "");
 
$myURL = $routing->generate("default", array("module" => "foo", "action" => "bar"));
$myURL = str_replace($urlPrefix, "", $myURL);
 
by Rich Sage on 2010-05-06, tagged phorum  route  url 
(2 comments)

Get module and action from an absolute url

I happened to come across this problem - parse referer to get its module and action. I thought there was an official way to do this in Symfony. But it turns out not(At least 1.0, maybe I didn't dig deep enough, then please inform me). I don't like to parse it by myself either, so after digging into symfony's source code. I come up with below solution:

function getModuleActionFrom($absurl) {
        $_uri = $_SERVER['REQUEST_URI'];
        if (isset($_SERVER['PATH_INFO'])) $_path = $_SERVER['PATH_INFO'];
        $_SERVER['REQUEST_URI'] = parse_url($absurl, PHP_URL_PATH);
        if (isset($_SERVER['PATH_INFO'])) $_SERVER['PATH_INFO'] = null;
        $tempReq = new sfWebRequest();
        $tempReq->initialize(sfContext::getInstance());
        $result  = array(
            'module' => $tempReq->getParameter('module'),
            'action' => $tempReq->getParameter('action')
        );
 
        $_SERVER['REQUEST_URI'] = $_uri;
        if (isset($_SERVER['PATH_INFO'])) $_SERVER['PATH_INFO'] = $_path;
        // I didn't notice sfRouting sets currentRouteName when it parses url, so ...
        $tempReq->initialize(sfContext::getInstance());
 
        return $result;
}
 

This function doesn't check whether the url belongs to the current application, as sfWebRequest doesn't do that.

by Kail Zhang on 2009-11-13, tagged url 

Change app by routing class

Create a file into lib/routing/sfChangeAppRoute.class.php and paste :

<?php
 
class sfChangeAppRoute extends sfRoute{
 
    public function generate($params, $context = array(), $absolute = true)
  {
 
    if(!array_key_exists('app',$params))
        return parent::generate($params, $context, $absolute);
 
 
    $app_name=sfContext::getInstance()->getConfiguration()->getApplication();
    $env_name=sfContext::getInstance()->getConfiguration()->getEnvironment();
    if($env_name=='prod')
        $env='';
    else
        $env='_'.$env_name;
 
    $url='/../'.$params['app'].$env.'.php';
 
    return $url;
  }
 
}
 
?>
 

Now into your routing.yml

app:
  url: /application/:app
  params: {  }
  class:  sfChangeAppRoute
 

And after that you could use the route @app?app=MY_other_app

by excessive demon on 2009-08-26, tagged application  routing  url 
(1 comment)

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 
(4 comments)

Compute public path

Here is how you can get public path to i.e. http://www.domain.com/flash/charts/chart.swf

Put this code into your template.

<?php
echo image_path('/flash/charts/chart.swf', true);
?>

Tnx Matt. This is the function I was looking for :)

by Damjan Malis on 2006-08-07, tagged url 
(3 comments)

Get the module, action and parameters from a url

The problem

You want to get the module, action and parameters associated to a given url, pretty much as the routing system does.

The solution

First you will have to remove the part of the url which is not symfony specific. That part typically looks like yoursite.com/path/to/symfony. Once you've done that execute the following code:

$r = new sfRouting();
$r->setRoutes(sfRouting::getInstance()->getRoutes());
$params = $r->parse($myUrl);
$module = $params['module'];
$action = $params['action'];

Now the module and action associated to this url are as above in $module and $action and the parameters are the remaining elements of the array $params.

by Olivier Verdier on 2006-07-21, tagged action  module  parameters  routing  url 
(12 comments)