Yii2 UrlManager and aliases from DB

Hi everyone.

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.

Sorry for my bad english.

I’ve found a solution here, if need translation - I could help.

Thank you. I’ve already lost hope that someone will answer.

PS: I don’t need translation. ty =)

Sorry but that’s not all, will get some info and reply asap.

this is my code for viewing by ‘alias’.


    

    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.

Yes, that replacement is exactly what I need.

I didn’t found solution yet.

There was one answer in russian branch of forum, but I haven’t check the proposed method due to lack of time.