Created
June 22, 2012 07:40
-
-
Save isanosian/2971068 to your computer and use it in GitHub Desktop.
Symfony 2: use GET parameter _path to determine route
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Package\MyBundle\Component\HttpFoundation\Request; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 (); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
router.options.generator_base_class: Package\MyBundle\Component\Routing\Generator\UrlGenerator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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¶m1=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