Skip to content

Instantly share code, notes, and snippets.

@isanosyan
Created June 22, 2012 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isanosyan/2971068 to your computer and use it in GitHub Desktop.
Save isanosyan/2971068 to your computer and use it in GitHub Desktop.
Symfony 2: use GET parameter _path to determine route
use Package\MyBundle\Component\HttpFoundation\Request;
<?php
namespace Package\MyBundle\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\Request as BaseRequest;
/**
* Represents HTTP request.
* Replaces actual path with GET _path parameter (if present)
*
* @author Ilya A. Sanosyan
*/
class Request extends BaseRequest
{
/**
* Returns the path being requested relative to the executed script.
* If _path GET parameter is present, returns value of this parameter.
*
* The path info should always start with a /.
*
* Suppose this request is instantiated from /mysite on localhost:
*
* * http://localhost/mysite returns an empty string
* * http://localhost/mysite/about returns '/about'
* * http://localhost/mysite/about?var=1 returns '/about'
* * http://localhost/mysite/about?var=1&_path=/my returns '/my'
*
* @return string
*/
function getPathInfo ()
{
$getQueryElements = array ();
parse_str ($this->server->get ('QUERY_STRING'), $getQueryElements);
if (isset ($getQueryElements ['_path']))
return $getQueryElements ['_path'];
return parent::getPathInfo ();
}
}
parameters:
router.options.generator_base_class: Package\MyBundle\Component\Routing\Generator\UrlGenerator
<?php
namespace Package\MyBundle\Component\Routing\Generator;
use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
/**
* Will be base url generator class for Router.
* Enables genrator to work with our type of URIs:
* /app.php?_path=/my/path&param1=v1&...
*
* @author Ilya A. Sanosyan
*/
class UrlGenerator extends BaseUrlGenerator
{
/**
* Asks parent class to generate URL and transforms it:
* - adds GET parameter _path holding path
* - replaces path with base url
* - adds app.php if no front controller was specified.
*/
protected function doGenerate ($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
{
// asking parent to generate URL and parsing it
$url = str_replace ($base = $this->context->getBaseUrl (),
'',
parent::doGenerate ($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
);
$urlParts = parse_url ($url);
// adding _path GET parameter
if (isset ($urlParts ['query']))
parse_str ($urlParts ['query'], $queryParameters);
else
$queryParameters = array ();
if (isset ($urlParts ['path']))
$queryParameters ['_path'] = $urlParts ['path'];
// adding base url and front controller name
if (!$base || mb_substr ($base, -1) == '/')
$base .= 'app.php';
// path should be empty
$urlParts ['path'] = $base;
// building uri back
$urlParts ['query'] = urldecode (http_build_query ($queryParameters));
return http_build_url ($urlParts);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment