I decided to learn Yii. And everything seems to be clear, but there is a question, the answer to which I can’t find.
I need to orginize dinamic URLs.
I have a page "Albums" with links on it like "album?id=[x]". For each page I have a field "alias" in database, where admin can enter any link. Actually the question: how to replace "?id=[x]" with "/[alias]"?
I would be grateful for explanation or links with a description of the solution.
public function actionView($alias)
{
return $this->render('view', [
'model' => $this->loadModelSlug($alias),
]);
}
protected function loadModelSlug($alias)
{
$model = Produse::findOne(['alias'=>$alias]);
if ($model === null)
throw new NotFoundHttpException('The requested page does not exist.');
return $model;
}
in your code you should replace Url::toRoute([‘view’,‘id’=>$model->id]) to Url::toRoute([‘view’,‘alias’=>$model->alias]).
the problem now remains to manage routes in UrlManager, that’s hard for me for now, if you find any solution I would appreciate.