Last active
August 29, 2015 14:05
-
-
Save serebro/c69fa96234c6fcebff09 to your computer and use it in GitHub Desktop.
Generate Phalcon URL with GET params
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 | |
class Url extends \Phalcon\Mvc\Url | |
{ | |
/** | |
* Generates a URL with GET params | |
* | |
* <code> | |
* | |
* $router->add('/{controller}/{action}/{id}')->setName('controller_action_id'); | |
* | |
* echo \Phalcon\Tag::linkTo(array( | |
* 'controller_action_id', // name of route or 'for' => 'controller_action_id' | |
* 'controller' => 'videos', | |
* 'action' => 'list', | |
* 'id' => 123, | |
* 'sort' => 'date', | |
* 'offset' => 25 | |
* ), 'Videos'); | |
* | |
* // Result: "/videos/list/123/?sort=date&offset=25" | |
* | |
* Volt syntax: | |
* <code> | |
* {{ link_to(['controller_action_id', 'controller' => 'videos', 'action' => 'list','id' => 123,'sort' => 'date','offset' => 25], 'Videos') }} | |
* </code> | |
* | |
* </code> | |
* | |
* @param null $uri | |
* @param null $args | |
* @param null $local | |
* @return string | |
*/ | |
public function get($uri = null, $args = null, $local = null) | |
{ | |
if (is_array($uri)) { | |
$used_params = ['for' => 0]; | |
if (!isset($uri['for'])) { | |
$uri['for'] = array_shift($uri); | |
} | |
/** @var \Phalcon\Mvc\Router $router */ | |
$router = $this->getDI()->get('router'); | |
if ($route = $router->getRouteByName($uri['for'])) { | |
$used_params += $route->getPaths(); | |
} | |
$args = (array)$args; | |
$args += array_diff_key($uri, $used_params); | |
} | |
return parent::get($uri, $args, $local); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment