P
P
Padre2018-02-06 18:22:14
Yii
Padre, 2018-02-06 18:22:14

How to pass url to Yii2 controller?

Let's say they access the site page. The domain.com/products/samsung-548z
request is passed to the show method in the products controller, obviously the model should get inside the method. samsung-548z but how to transfer it there?

public function actionShow($model)
    {
         var_dump($model);
    }

Says Bad Request (#400) Missing required parameters: model
Where and how can I pick it up?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Dart, 2018-02-06
@yii16

In general, $model = Yii::$app->request->get();what you did depends on what is written in your rules

M
Maxim Fedorov, 2018-02-06
@Maksclub

You need to write a rule in the urlManager in the main.php config (or web.php for basic)
'products/<model:[\w\-]+>' => 'products/show',

M
Maxim Fedorov, 2018-02-06
@qonand

Write the corresponding rule, an example can be viewed here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question