Code snippets for symfony 1.x

Navigation

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 

Comments on this snippet

gravatar icon
#1 brikou on 2006-12-06 at 09:47

great but unfortunalyy this doesn't work with absolute url... I wish i could retrieve easily parameter from this->getRequest()->getReferer();

gravatar icon
#2 Dawit Abraham on 2008-01-23 at 03:17

You could do this for absolute url:

[php] $referer = sfContext::getInstance()->getRequest()->getReferer(); $environment = sfConfig::get('sf_environment'); $script = ($environment == 'prod') ? 'index.php': 'frontend_dev.php'; $referer = substr($referer, strpos($referer, $script) + strlen($script)); $referer = str_replace('/'.$script, '', $referer);

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

gravatar icon
#3 Dawit Abraham on 2008-01-23 at 03:27

Sorry about the last entry. A user should be able to edit his/her own comments here.

$referer = sfContext::getInstance()->getRequest()->getReferer();
$environment = sfConfig::get('sf_environment');
$script = ($environment == 'prod') ? 'index.php': 'frontend_dev.php';
$referer = substr($referer, strpos($referer, $script) + strlen($script));
$referer = str_replace('/'.$script, '', $referer);
 
$r = new sfRouting();
$r->setRoutes(sfRouting::getInstance()->getRoutes());
$params = $r->parse($referer);
$module = $params['module'];
$action = $params['action'];
 
gravatar icon
#4 Stig Manning on 2009-08-24 at 03:24

The above code does not work for Symfony 1.1 and 1.2 (only 1.0) as the routing system was actually overhauled and made easier to use. Great news, but terrible documentation on how to use this new routing system.

To get the module/action and params from URL, you can now go:

<?php $params = sfContext::getInstance()->getRouting()->parse($myUrl); $module = $params['module']; $action = $params['action']; ?>

Regards, Stig

gravatar icon
#5 Stig Manning on 2009-08-24 at 03:27

Sorry code again:

$params = sfContext::getInstance()->getRouting()->parse($myUrl);
$module = $params['module'];
$action = $params['action'];
 
gravatar icon
#6 Ricardo Oliveira on 2009-11-03 at 02:11

[quote]$script = ($environment == 'prod') ? 'index.php': 'frontend_dev.php';[/quote]

Well, most prod environments have no_script_name : on , so the referer string doesn't have 'index.php' in it. Any clues on how to get it working?

gravatar icon
#7 Mathias Vitalis on 2009-11-18 at 06:50

This works for me : <code> $referer = sfContext::getInstance()->getRequest()->getReferer(); $environment = sfConfig::get('sf_environment'); $parseurl = parse_url($referer); $script = ($environment == 'prod') ? $parseurl['host'] : 'frontend_dev.php'; $referer = substr($referer, strpos($referer, $script) + strlen($script)); $referer = str_replace('/'.$script, '', $referer);

$params = sfContext::getInstance()-&gt;getRouting()-&gt;parse($referer);
$module = $params[&#039;module&#039;];
$action = $params[&#039;action&#039;];
 

</code>

gravatar icon
#8 Mathias Vitalis on 2009-11-18 at 06:51

Sorry :

$referer = sfContext::getInstance()-&gt;getRequest()-&gt;getReferer();
$environment = sfConfig::get(&#039;sf_environment&#039;);
$parseurl = parse_url($referer);
$script = ($environment == &#039;prod&#039;) ? $parseurl[&#039;host&#039;] : &#039;frontend_dev.php&#039;;
$referer = substr($referer, strpos($referer, $script) + strlen($script));
$referer = str_replace(&#039;/&#039;.$script, &#039;&#039;, $referer);
$params = sfContext::getInstance()-&gt;getRouting()-&gt;parse($referer);
$module = $params[&#039;module&#039;];
$action = $params[&#039;action&#039;];
 
gravatar icon
#9 Lorand Tamas on 2010-05-20 at 11:36

I try this solution to get the referer action and module, but looks like i can not use the cache for the actual action. (My cache.yml configuration is ignored for this action)

gravatar icon
#10 Lorand Tamas on 2010-05-20 at 11:51

By using this: $params = sfContext::getInstance()->getRouting()->findRoute($referer); instead of this: $params = sfContext::getInstance()->getRouting()->parse($referer); # This ignores the cache config and getting the module name and action name by: $params['parameters']['module'] and $params['parameters']['action'] my problem was resolved.

The cache now working well.

gravatar icon
#11 Lorand Tamas on 2010-05-20 at 11:54

One more time:

By using this:

$params = sfContext::getInstance()->getRouting()->findRoute($referer);

instead of this:

$params = sfContext::getInstance()->getRouting()->parse($referer); # This ignores the cache config

and getting the module name and action name by:

$params['parameters']['module']

and

$params['parameters']['action']

my problem was resolved.

The cache now working well.

gravatar icon
#12 Anonymous on 2011-09-07 at 01:36

$referer = $request->getReferer();

if (strlen($referer) &gt; 0)
{
  $pathInfo = $request-&gt;getPathInfoArray();
  $referer = str_replace(array (
      &#039;http://&#039;, 
      &#039;https://&#039;), &#039;&#039;, $referer);
  $referer = str_replace($pathInfo[&#039;HTTP_HOST&#039;], &#039;&#039;, $referer);
  $referer = str_replace($pathInfo[&#039;SCRIPT_NAME&#039;], &#039;&#039;, $referer);
  $params = $this-&gt;getContext()-&gt;getRouting()-&gt;parse($referer);
  $module = $params[&#039;module&#039;];
  $action = $params[&#039;action&#039;];
}