ako spravne napisat route
- MKI-Miro
- Člen | 277
Ahojte
Nejak nie a nie pochopit ten navod ktory je v dokumentacii ako pisat routy,
Mam takyto presenter:
class ProductPresenter extends BasePresenter {
public function renderAuthor($id) {
$this->template->id = $id;
$puzzles = $this->puzzleRepository->
findAll()->
where(array("category_author_id" =>$id))->
order("name ASC");
$this->template->puzzles = $puzzles;
$author = $this->category_authorRepository->findById($id);
$this->template->author = $author;
}
Teraz je url v tvare /product/author/id
A chcel by som aby som v tvare len /author-name napr /vincent-van-gogh
pricom meno author je v $author[„name“]
Viete mi prosim pomoct ako spravne napisat routu ?
Dakujem
Editoval MKI-Miro (15. 4. 2014 9:20)
- Oli
- Člen | 1215
Viděl jsi tuhle přednášku? Na tom jsem toho hodně pochopil.
Obecně, musíš si nastavit author jako defaultní action v routování (potom se ti nebude vypisovat) nebo tam dat jinou jedinecnou podminku, podle ceho se ta routa ma poznat. A $id si pomoci filter_in a filter_out přefiltrovat aby do presenteru vracelo id a do url slug.
- MKI-Miro
- Člen | 277
vdaka, pozrel som si prednasku a spravil som to takto:
fungovat to funguje len nie som si isty ci je to aj spravny sposob
<?php
use Nette\Application\Routers\RouteList,
Nette\Application\Routers\Route,
Nette\Application\Routers\SimpleRouter;
/**
* Router factory.
*/
class RouterFactory {
public $category_authorRepository;
public function __construct(Puzzlemania\category_authorRepository $category_authorRepository) {
$this->category_authorRepository = $category_authorRepository;
}
/**
* @return Nette\Application\IRouter
*/
public function createRouter() {
$router = new RouteList();
$router[] = new Route('index.php', 'Homepage:default', Route::ONE_WAY);
$router[] = new Route("<id>", array(
"presenter" => "Product",
"action" => "author",
"id" => array(
Route::FILTER_IN => function($id) {
if (is_numeric($id))
{
return $id;
} else {
$author = $this->category_authorRepository->findAll()->where(array("name" => $id))->fetch();
return $author->id;
}
},
Route::FILTER_OUT => function($id) {
if (!is_numeric($id))
{
return $id;
} else {
$author = $this->category_authorRepository->findById($id);
//$alias = "alias".$this->lang;
return $author["name"];
}
}
)
));
$router[] = new Route('<presenter>/<action>[/<id>]', 'Homepage:default');
return $router;
}
}
plus by som mal otazku, ako v RouterFactory pristup k perzistentnej premenj $lang ktora je definova v BasePresenter ?
dakujem
- Jan Suchánek
- Člen | 404
Třebas:
$router[] = new Route("[<lang cs|en {2}>/]<id>", array(
"lang" => "cs",
"presenter" => "Product",
"action" => "author",
"id" => NULL,
NULL => array(
Route::FILTER_IN => function($params) {
...
},
Route::FILTER_OUT => function($params) {
...
}
)
...
Editoval jenicek (30. 4. 2014 14:39)
- llsm
- Člen | 121
MKI-Miro napsal(a):
plus by som mal otazku, ako v RouterFactory pristup k perzistentnej premenj $lang ktora je definova v BasePresenter ?
Muzes i stejne jako pracujes s $id, tj.:
<?php
$router[] = new Route("[<lang cs|en {2}>/]<id>", array(
"lang" => "cs",
"presenter" => "Product",
"action" => "author",
"id" => array(
Route::FILTER_IN => function($id) {
...
},
Route::FILTER_OUT => function($id) {
...
}
"lang" => array(
Route::FILTER_IN => function($lang) {
...
},
Route::FILTER_OUT => function($lang) {
...
}
)
?>