E
E
Evgenya-k22019-04-01 15:34:09
Yii
Evgenya-k2, 2019-04-01 15:34:09

How to write a regular expression for a URL?

I set up an advanced CNC in the project, for viewing pages of one news or one project in the controller, one action view is used, because the template is the same:

public function actionView($className, $id, $title)
    {
        switch ($title) {
            case 0:
                $title = 'Проекты';
                $route = 'site/projects';
                break;
            case 1:
                $title = 'Новости';
                $route = 'site/news';
                break;
        }

        if (($model = $className::findOne($id)) !== null) {
            return $this->render('view', [
                'model' => $model,
                'title' => $title,
                'route' => $route,
            ]);
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

The default url is:
site/view?className=common%5Cmodels%5CNews&id=1&title=1
.
In the final version, I want to get the name of the model (the last word in className is News) and id, like:
/news-1
Tell me, how can I do it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question