Skip to content

Instantly share code, notes, and snippets.

@matej21
Last active December 8, 2016 14:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save matej21/8118567 to your computer and use it in GitHub Desktop.
Save matej21/8118567 to your computer and use it in GitHub Desktop.
<?php
use Nette\Application\IRouter;
use Nette\Application\UI\InvalidLinkException;
use Nette\Application\UI\Presenter;
use Nette\Application;
use Nette\Http\Url;
use Nette\Object;
/**
* @author David Matějka
* @author David Grudl
*/
class UrlGenerator 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($presenter, 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;
}
}
@simPod
Copy link

simPod commented Jan 16, 2015

Ahoj, prosím, jak tohle správně použít? Zaregistroval jsem jako service do config.neon: - UrlGenerator a pak volám v konstruktoru modelu public function __construct(Nette\Http\Request $request, UrlGenerator $urlGenerator) {

Dostávám hlášku Service '84_UrlGenerator': Service of type Nette\Http\Url needed by UrlGenerator::__construct() not found. Did you register it in configuration file? Dělám něco blbě? Nette\Http\Request jsem taky nemusel registrovat a ten funguje.... Díky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment