K
K
Kekoc2015-02-06 21:27:38
PHP
Kekoc, 2015-02-06 21:27:38

How to change yii2 Get request via urlmanager?

Hello.
You need to change the type of get request to the controller function. There is a controller and an actionSay function

public function actionSay($message="Hello world"){

        return $this->render('say',['messager'=>$message,'meta'=>'METATEST']);
    }

Now it is done like this.
mysite/say?message=123
Message 123 will be displayed in the view
How can I change the urlManager to make the request look like this -> mysite/say/123 or
mysite/say/message/123 ?
'components' => [

        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName'=>false,
            'enableStrictParsing' => true,
          //  'class' => 'yii\web\UrlManager',
           'rules' => [
               '<action>'=>'site/<action>',
               '' => 'site/index',

            ],
            // ...
        ],

This is what urlManager looks like

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Terminaft, 2015-02-07
@Kekoc

In config:

'rules' => [
     '' => 'site/index', // можно убрать и задать 'defaultRoute' => 'site/index' в конфиге
     'say/<message>' => 'site/say',
     //'say/message/<message>' => 'site/say',
],

In controller:
public function actionSay($message) // не уверен насчет задания значения по умолчанию, надо Вам проверить
{
     return $this->render('say', ['messager' => $message, 'meta' => 'METATEST']);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question