1

Здравствуйте. Подскажите пожалуйста, как убрать id из адресной строки? Что бы было не так, http://poll.ua/gonki/gonki-na-dronah/16, а вот так http://poll.ua/gonki/gonki-na-dronah. В URLmanager в rules, у меня следующее:

'<slug>/<slug1>/<poll_id:\d+>' => 'site/competition/',

И статьи я выбираю через poll_id:

public function actionCompetition($poll_id)
{
     $comp = ACompetition::findOne($poll_id);
     return $this->render('competition', [
                'comp' => $comp,
                ];
}
1
  • Надо описать метод, который будет получать данные по ЧПУ вместо findOne 7 янв 2017 в 8:14

1 ответ 1

2

Я это реализовывал вот так:

С учётом того что у нас есть таблица с категориями Category, у каждой категории есть поле alias(в вашем варианте это категория с alias = 'gonki'), а в таблице ACompetition у нас только ссылка на категорию по id.

Правило в UrlManager:

'<category:[\w\-]+>/<alias:[\w\-]+>' => 'site/competition/'

Action:

public function actionCompetition($category, $alias)
{
     $category = Category::findOne(['alias' => $category]);
     $comp = ACompetition::findOne(['category_id' => $category->id, 'alias' => $alias]);

     return $this->render('competition', [
         'comp' => $comp,
     ];
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.