Skip to content
Permalink
Browse files

routing

  • Loading branch information...
dg committed Feb 20, 2019
1 parent daa4a6d commit d0c2dd0cd7b0f45158ef9db8e7865f724b98047c
Showing with 38 additions and 40 deletions.
  1. +19 −20 cs/routing.texy
  2. +19 −20 en/routing.texy
@@ -15,10 +15,10 @@ Routování je obousměrné překládání mezi URL a akcí presenteru. Obousmě
Co je routování?
================

Routování je obousměrné překládání mezi URL a **aplikačním požadavkem**.
Routování je obousměrné překládání mezi URL a **polem parametrů**.

- `Nette\Http\IRequest` (obsahuje URL) -> `Nette\Application\Request`
- `Nette\Application\Request` -> absolutní URL
- `Nette\Http\IRequest` (obsahuje URL) -> pole
- pole -> absolutní URL

Díky obousměrnému routování už nemusíme do šablon natvrdo zapisovat URL, odkazujeme se pouze na [akce presenterů |presenters#zpracovani-akce-presenteru] a framework nám už URL vygeneruje sám:

@@ -296,9 +296,9 @@ Jednosměrné routy se používají zejména pro zachování funkčnosti starýc

/--php
// staré URL /product-info?id=123
$router[] = new Route('product-info', 'Product:detail', Route::ONE_WAY);
$router->addRoute('product-info', 'Product:detail', $router::ONE_WAY);
// nové URL /product/123
$router[] = new Route('product/<id>', 'Product:detail');
$router->addRoute('product/<id>', 'Product:detail');
\--

Navíc dojde k automatickému přesměrování na nový tvar URL, takže vám tyto stránky vyhledávače nezaindexují dvakrát (viz [#SEO a kanonizace]).
@@ -476,9 +476,9 @@ class RouterFactory
public static function createRouter(): RouteList
{
$router = new RouteList;
$router[] = new Route('article/<id>', 'Article:view');
$router[] = new Route('rss.xml', 'Feed:rss');
$router[] = new Route('<presenter>/<action>', 'Homepage:default');
$router->addRoute('article/<id>', 'Article:view');
$router->addRoute('rss.xml', 'Feed:rss');
$router->addRoute('<presenter>/<action>', 'Homepage:default');
return $router;
}
}
@@ -506,7 +506,7 @@ class RouterFactory
public static function createRouter(Nette\Database\Connection $db, bool $debugMode = false): RouteList
{
$router = new RouteList;
$router[] = ...
$router->add(...);
return $router;
}
}
@@ -533,17 +533,17 @@ class RouterFactory
public static function createRouter(): RouteList
{
$router = new RouteList;
$router[] = self::createForumRouter();
$router->add(self::createForumRouter());
...
$router[] = new Route('<presenter>/<action>', 'Homepage:default');
$router->addRoute('<presenter>/<action>', 'Homepage:default');
return $router;
}

public static function createForumRouter(): RouteList
{
$router = new RouteList('Forum');
// http://forum.example.com/homepage/default se mapuje na presenter Forum:Homepage
$router[] = new Route('//forum.example.com/<presenter>/<action>');
$router->addRoute('//forum.example.com/<presenter>/<action>');
return $router;
}
}
@@ -584,30 +584,29 @@ K přesměrování nedojde při AJAXovém nebo POST požadavku, protože by doš
Vlastní router
==============

Pokud vám nabízené routery nedostačují, můžete si vytvořit router vlastní a zcela přirozeně ho začlenit do kolekce rout. Router je implementací rozhraní [IRouter |api:Nette\Application\IRouter] se dvěma metodami:
Pokud vám nabízené routery nedostačují, můžete si vytvořit router vlastní a zcela přirozeně ho začlenit do kolekce rout. Router je implementací rozhraní [Router |api:Nette\Routing\Router] se dvěma metodami:

/--php
use Nette\Application\Request as AppRequest;
use Nette\Http\IRequest as HttpRequest;
use Nette\Http\Url;
use Nette\Http\UrlScript;

class MyRouter implements Nette\Application\IRouter
class MyRouter implements Nette\Routing\Router
{
public function match(HttpRequest $httpRequest): ?AppRequest
public function match(HttpRequest $httpRequest): ?array
{
// ...
}

public function constructUrl(AppRequest $appRequest, Url $refUrl): ?string
public function constructUrl(array $params, UrlScript $refUrl): ?string
{
// ...
}
}
\--

Metoda `match` zpracuje aktuální požadavek [$httpRequest |api:Nette\Http\Request] (ze kterého lze získat nejen URL) do interního požadavku [Nette\Application\Request |api:Nette\Application\Request] obsahující jméno presenteru a jeho parametry. Pokud požadavek zpracovat neumí, vrátí null.
Metoda `match` zpracuje aktuální požadavek [$httpRequest |api:Nette\Http\Request] (ze kterého lze získat nejen URL) do pole obsahující jméno presenteru a jeho parametry. Pokud požadavek zpracovat neumí, vrátí null.

Metoda `constructUrl` naopak sestaví z interního požadavku výsledné absolutní URL. K tomu může využít informace z parametru `$refUrl`.
Metoda `constructUrl` naopak sestaví z pole parametrů výsledné absolutní URL. K tomu může využít informace z parametru `$refUrl`.

Možnosti vlastního routeru jsou prakticky neomezené, lze třeba implementovat router, který bude routovat na základě databázové tabulky.

@@ -15,10 +15,10 @@ Routing is a two-way conversion between URL and presenter action. *Two-way* mean
What is Routing?
================

Routing is a **two-way conversion** between **URL** and an **application request**.
Routing is a **two-way conversion** between **URL** and an array of parameters.

- `Nette\Http\IRequest` (includes URL) -> `Nette\Application\Request`
- `Nette\Application\Request` -> absolute URL
- `Nette\Http\IRequest` (includes URL) -> array
- array -> absolute URL


Thanks to bidirectional routing you don't have to hardcode URLs into templates anymore, you simply link to [presenters' actions|presenters#handling-presenter-actions] and framework generates the URLs for you:
@@ -300,8 +300,8 @@ Unidirectional routers are especially used to preserve the functionality of old

/--php
// old URL /product-info?id=123, new URL /product/123
$router[] = new Route('product-info', 'Product:detail', Route::ONE_WAY);
$router[] = new Route('product/<id>', 'Product:detail');
$router->addRoute('product-info', 'Product:detail', $router::ONE_WAY);
$router->addRoute('product/<id>', 'Product:detail');
\--

Additionally, automatic redirection to the new URL form will cause to do not index the search engines twice (see [#SEO and canonization]).
@@ -478,9 +478,9 @@ class RouterFactory
public static function createRouter(): RouteList
{
$router = new RouteList;
$router[] = new Route('article/<id>', 'Article:view');
$router[] = new Route('rss.xml', 'Feed:rss');
$router[] = new Route('<presenter>/<action>', 'Homepage:default');
$router->addRoute('article/<id>', 'Article:view');
$router->addRoute('rss.xml', 'Feed:rss');
$router->addRoute('<presenter>/<action>', 'Homepage:default');
return $router;
}
}
@@ -508,7 +508,7 @@ class RouterFactory
public static function createRouter(Nette\Database\Connection $db, bool $debugMode = false): RouteList
{
$router = new RouteList;
$router[] = ...
$router->add(...);
return $router;
}
}
@@ -536,17 +536,17 @@ class RouterFactory
public static function createRouter(): RouteList
{
$router = new RouteList;
$router[] = self::createForumRouter();
$router->add(self::createForumRouter());
...
$router[] = new Route('<presenter>/<action>', 'Homepage:default');
$router->addRoute('<presenter>/<action>', 'Homepage:default');
return $router;
}

public static function createForumRouter(): RouteList
{
$router = new RouteList('Forum');
// http://forum.example.com/homepage/default maps to presenter Forum:Homepage
$router[] = new Route('//forum.example.com/<presenter>/<action>');
$router->addRoute('//forum.example.com/<presenter>/<action>');
return $router;
}
}
@@ -584,30 +584,29 @@ Ajax and POST requests are not redirected as user would suffer either a data los
Custom Router
=============

If these offered routes do not fit your needs, you may create your own router and add it to your *router collection*. Router is nothing more than an implementation of [IRouter |api:Nette\Application\IRouter] with it's two methods:
If these offered routes do not fit your needs, you may create your own router and add it to your *router collection*. Router is nothing more than an implementation of [Router |api:Nette\Routing\Router] with it's two methods:

/--php
use Nette\Application\Request as AppRequest;
use Nette\Http\IRequest as HttpRequest;
use Nette\Http\Url;
use Nette\Http\UrlScript;

class MyRouter implements Nette\Application\IRouter
class MyRouter implements Nette\Routing\Router
{
public function match(HttpRequest $httpRequest): ?AppRequest
public function match(HttpRequest $httpRequest): ?array
{
// ...
}

public function constructUrl(AppRequest $appRequest, Url $refUrl): ?string
public function constructUrl(array $params, UrlScript $refUrl): ?string
{
// ...
}
}
\--

Method `match` does process an [HttpRequest |api:Nette\Http\Request] (which offers more than just a [Url |api:Nette\Http\Url]) into an internal [Nette\Application\Request |api:Nette\Application\Request] which contains presenter name and it's parameters. If the HTTP request could not be processed, it should return `null`.
Method `match` does process an [HttpRequest |api:Nette\Http\Request] (which offers more than just a [Url |api:Nette\Http\Url]) into an array which contains presenter name and it's parameters. If the HTTP request could not be processed, it should return `null`.

Method `constructUrl` generates an absolute URL from application request, possibly utilizing information from `$refUrl` argument.
Method `constructUrl` generates an absolute URL from array of parameters, possibly utilizing information from `$refUrl` argument.

Possibilities of custom routers are unlimited, for example it's possible to implement a router based on database records.

0 comments on commit d0c2dd0

Please sign in to comment.
You can’t perform that action at this time.