Snippets

Create an account or login to be able to add, comment and rate snippets.

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'];
 
You need to create an account or log in to post a comment or rate this snippet.