phpcontrolleryii2pretty-urls

Yii2: how to call action in url with use "pretty url"


Without pretty url route has view http://192.168.100.5/index.php?r=tweet/statistic&from=20160320&to=20160325 and it`s work well.

As docs say when 'enablePrettyUrl' => true /tweet call default action - and it`s work well also

But for other actions route should be /tweet/statistic. But there is 404 error.

How i can call app\controllers\TweetController actionStatistic() in this case?

added: i use basic template

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule',
         'controller' => 'tweet'],
        'GET tweet/statistic' => 'tweet/statistic'
    ],
],

when i try curl request for http://192.168.100.5/tweet/statistic

HTTP/1.1 404 Not Found

If 'enablePrettyUrl' => false and http://192.168.100.5/index.php?r=tweet/statistic" it`s work well


Solution

  • Please mention that this is a rest API, as this changes things a lot. The point is you have to declare the rules so Yii2 will now know how to handle them. You have to tell it what type of request this will be and where the request will go. This is quite different compared to the normal application because this is a rest API.

    My working config:

     'urlManager' => [
                'enablePrettyUrl' => true,
                'enableStrictParsing' => true,
                'showScriptName' => false,
                'rules' => [
                    [
                        'class' => 'yii\rest\UrlRule',
                        'controller' => [
                            'v1/client',
    ..........................
                        ]
                    ],
                    'GET v1/clients/info' => 'v1/client/info',
                    'POST v1/settings/suburb' => 'v1/setting/suburb',
                ],
            ],