A
A
alex995052018-03-19 11:13:10
Yii
alex99505, 2018-03-19 11:13:10

How to get GET parameters in Route Yii?

I want to get parameters in the GET controller in the standard way:

Yii::$app->request->get('aID', '');
Yii::$app->request->get('aTitle', '');

Found only hardcode
Request
test.com/article/description?aID=323&aTitle=teaser...
rule
'article/description/<aID:\d+>/<aTitle>' => 'article/description'

But what if some variables will not always exist, for example, the moment when aTitle will be absent. How then, to create two rules?
But what if aID and aTitle are swapped in the request, will the same rule also work normally?
'article/description/<aID:\d+>/<aTitle>' => 'article/description'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
davidnum95, 2018-03-19
@alex99505

You need to distinguish between path and query parameters.
For example:
Here aID=323&aTitle=teaser+of+this+articleit is the query parameters.
Accordingly, they do not need to be specified in the route:
They are implicitly passed to the action.
In this case, all query parameters will be required. To change this, you need to set the default values:

public function actionDescription($aID, $aTitle = null, $something = 'default', $else = 1) {}

O
OKyJIucT, 2018-03-19
@OKyJIucT

Create URLs with Url::to, and always specify the same order of parameters, and everything will work correctly. then you can get by with just two rules in the UrlManager.
Or create your own URL parsing rules and be able to process them even if the order is arbitrary. A rough example can be found here https://yiiframework.com.ua/ru/doc/guide/2/runtime...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question