Код: Выделить всё
public function index_by_date($year = NULL, $month = NULL, $day = NULL) {
if($year && $month && $day) {
//Posts of day
}
elseif($year && $month) {
//Posts of month
}
elseif($year) {
//Posts of year
}
else {
//Exception...
}
}
Код: Выделить всё
mysite.com/posts/2016/06/11Код: Выделить всё
mysite.com/posts/2016/06Код: Выделить всё
mysite.com/posts/2016Теперь я хочу написать единый маршрут для всех эти случаи. Тогда второй и третий параметр должны быть необязательными.
Я пробовал что-то подобное, но это не работает:
Код: Выделить всё
$routes->connect('/posts/:year/:month/:day',
['controller' => 'Posts', 'action' => 'index_by_date'],
[
'year' => '[12][0-9]{3}',
'month' => '(0[1-9]|1[012])?',
'day' => '(0[1-9]|[12][0-9]|3[01])?',
'pass' => ['year', 'month', 'day']
]
);
РЕДАКТИРОВАТЬ
На данный момент это лучшее, что я мог сделать:
Маршрут:
Код: Выделить всё
$routes->connect('/posts/:date',
['controller' => 'Posts', 'action' => 'index_by_date'],
[
'date' => '\d{4}(\/\d{2}(\/\d{2})?)?',
'pass' => ['date']
]
);
Код: Выделить всё
public function index_by_date($date = NULL) {
list($year, $month, $day) = array_merge(explode('/', $date), [NULL, NULL, NULL]);
if($year && $month && $day) {
//Posts of day
}
elseif($year && $month) {
//Posts of month
}
elseif($year) {
//Posts of year
}
else {
//Exception...
}
}
Подробнее здесь: https://stackoverflow.com/questions/377 ... parameters
Мобильная версия