![]() |
|
Code snippets for symfony 1.x |
|
You want to get the module, action and parameters associated to a given url, pretty much as the routing system does.
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.
Comments on this snippet
great but unfortunalyy this doesn't work with absolute url... I wish i could retrieve easily parameter from this->getRequest()->getReferer();
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]
Sorry about the last entry. A user should be able to edit his/her own comments here.
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
Sorry code again:
[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?
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);
</code>
Sorry :
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)
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.
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.
$referer = $request->getReferer();