Automaticke doplnenie chybajuceho parametra v URL
- boob
- Člen | 21
zdravim,
snazim sa spravit nieco podobne ako blog, rad by som mal URL v tvare
example.com/2011/12/24/nejaky-nazov-clanku/ -> Fact::show - zobrazi clanok
example.com/2011/12/ -> Fact::showMonth - zobrazi clanky z daneho mesiaca
example.com/2011/ -> Fact::showYear - zobrazi clanky z daneho roka
example.com/ -> presmeruje na dnesny datum, teda example.com/2011/12/26/nadpis-tohto-clanku/
spravil som si v bootstrape tieto routy
<?php
$router[] = new Route('index.php', 'Fact:today', Route::ONE_WAY);
$router[] = new Route('<presenter Facts>/<action today>', 'Fact:today');
$router[] = new Route('<year [0-9]{4}>/<month [0-9]{1,2}>/<day [0-9]{1,2}>/[<caption [a-z-]+>/]', array(
'presenter' => 'Fact',
'action' => 'show'
));
$router[] = new Route('<year [0-9]{4}>/<month [0-9]{1,2}>/', 'Facts:showMonth');
$router[] = new Route('<year [0-9]{4}>/', 'Facts:showYear');
?>
FactPresenter
<?php
public function actionShow($year, $month, $day)
{
*** rozny kod ***
}
/* pri Today presmeruj na Show s dnesnym datumom */
public function actionToday()
{
$date = new DateTime();
$factsModel = new FactsModel();
/* tuto neviem, ci 301 je v poriadku, kedze kazdy den
to bude presmerovavat na inu URL */
$this->redirect(301, 'show', array(
'year' => $date->format('Y'),
'month' => $date->format('m'),
'day' => $date->format('d'),
'caption' => $factsModel->getFactCaption($date)
));
}
?>
teraz mi to viacmenej funguje s tym, ze ak zadam
example.com/2011/12/25/bla-blabla/
alebo
example.com/2011/12/25/
, tak mi zobrazi zaznam z 25.12., ale
v URL ostane bud bla-blabla
alebo nic
ja by som prave potreboval, aby ked zadam
example.com/2011/12/25/bla-blabla/
, aby mi to opravilo na skutocny
nazov zaznamu, teda example.com/2011/12/25/nejaky-nadpis/
(ked
nezadam nic, aby mi tam doplnilo ten nazov = aby vzdy bola URL s datumom aj
spravnym nadpisom)
trapil som sa s FILTER_IN/OUT a hladal na fore, ale nejako sa mi nedarlo :)
pripadne, ak by sa dalo pomocou tychto filtrov aj priamo pri route skontrolovat, ci pre dany datum existuje zaznam a ak nie, tak zmenit napr na aktualny den
dufam, ze som to napisal zrozumitelne a snad mi niekto bude vediet poradit :) vdaka
PS: posledne Nette z 12.12.2011 s PHP 5.3
EDIT // rozmyslam, ze asi v Show by som mohol skontrolovat ten nazov a redirectom to pripadne zmenit, ze ? ale dalo by sa to cez tie routy ?
EDIT2 // skusil som to takto v actionShow
, vyzera to funkcne,
ale je to dobre riesenie ? :)
<?php
$factsModel = new FactsModel();
$real_caption = $factsModel->getFactCaption($this->date);
/* $caption je argument v actionShow */
if ($caption != $real_caption)
{
$this->redirect(301, 'show', array(
'year' => $this->date->format('Y'),
'month' => $this->date->format('m'),
'day' => $this->date->format('d'),
'caption' => $real_caption)
);
}
?>
Editoval boob (26. 12. 2011 16:11)
- Filip Procházka
- Moderator | 4668
Zkusil bych něco jako
class FactsPresenter extends BasePresenter
{
public function actionShow($year, $month, $day, $caption = NULL)
{
try {
$date = Nette\Datetime::from("$year-$month-$day");
} catch (\Exception $e) {
throw new Nette\Application\BadRequestException("Wrong date format", NULL, $e);
}
$factsModel = new FactsModel();
$dbCaption = $factsModel->getFactCaption($date);
if ($dbCaption !== $caption) {
$this->redirect('this', $year, $month, $day, $dbCaption);
}
// ...
}
}
Editoval HosipLan (27. 12. 2011 15:43)