Skip to content

Instantly share code, notes, and snippets.

@matej21
Last active December 8, 2016 14:04
Show Gist options
  • Select an option

  • Save matej21/8118567 to your computer and use it in GitHub Desktop.

Select an option

Save matej21/8118567 to your computer and use it in GitHub Desktop.

Revisions

  1. matej21 revised this gist Mar 10, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions UrlGenerator.php
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    use Nette\Application\IRouter;
    use Nette\Application\UI\InvalidLinkException;
    use Nette\Application\UI\Presenter;
    use Nette\Application;
    use Nette\Http\Url;
    use Nette\Object;
  2. matej21 revised this gist Feb 22, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UrlGenerator.php
    Original file line number Diff line number Diff line change
    @@ -69,7 +69,7 @@ public function link($destination, array $args = array())
    $action = (string) substr($destination, $a + 1);
    $presenter = substr($destination, 0, $a);
    if ($presenter[0] == ":") {
    $presenter = substr($destination, 1);
    $presenter = substr($presenter, 1);
    }
    if (!$action) {
    $action = 'default';
  3. matej21 revised this gist Dec 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UrlGenerator.php
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    * @author David Matějka
    * @author David Grudl
    */
    class LinkGenerator extends Object
    class UrlGenerator extends Object
    {

    /** @var \Nette\Application\IRouter */
  4. matej21 created this gist Dec 24, 2013.
    98 changes: 98 additions & 0 deletions UrlGenerator.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    <?php

    use Nette\Application\IRouter;
    use Nette\Application\UI\InvalidLinkException;
    use Nette\Application;
    use Nette\Http\Url;
    use Nette\Object;

    /**
    * @author David Matějka
    * @author David Grudl
    */
    class LinkGenerator extends Object
    {

    /** @var \Nette\Application\IRouter */
    protected $router;

    /** @var \Nette\Http\Url */
    protected $refUrl;


    /**
    * @param Url $refUrl
    * @param IRouter $router
    */
    public function __construct(Url $refUrl, IRouter $router)
    {
    $this->refUrl = $refUrl;
    $this->router = $router;
    }


    /**
    * URL factory.
    *
    * @param string $destination in format "[module:]presenter:action"
    * @param array $args array of arguments
    * @return string URL
    * @throws InvalidLinkException
    */
    public function link($destination, array $args = array())
    {
    $a = strpos($destination, '#');
    if ($a === FALSE) {
    $fragment = '';
    } else {
    $fragment = substr($destination, $a);
    $destination = substr($destination, 0, $a);
    }

    $a = strpos($destination, '?');
    if ($a !== FALSE) {
    parse_str(substr($destination, $a + 1), $args); // requires disabled magic quotes
    $destination = substr($destination, 0, $a);
    }

    $a = strpos($destination, '//');
    if ($a !== FALSE) {
    $destination = substr($destination, $a + 2);
    }


    if ($destination == NULL) { // intentionally ==
    throw new InvalidLinkException("Destination must be non-empty string.");
    }

    $a = strrpos($destination, ':');
    $action = (string) substr($destination, $a + 1);
    $presenter = substr($destination, 0, $a);
    if ($presenter[0] == ":") {
    $presenter = substr($destination, 1);
    }
    if (!$action) {
    $action = 'default';
    }

    $args[Presenter::ACTION_KEY] = $action;

    $request = new Application\Request(
    $presenter,
    Application\Request::FORWARD,
    $args,
    array(),
    array()
    );

    $url = $this->router->constructUrl($request, $this->refUrl);
    if ($url === NULL) {
    unset($args[Presenter::ACTION_KEY]);
    $params = urldecode(http_build_query($args, NULL, ', '));
    throw new InvalidLinkException("No route for $presenter:$action($params)");
    }

    return $url . $fragment;
    }

    }