yii2yii2-advanced-appyii-rest

rest yii2 pretty url urlManager


My yii2 rest work fine with this request http://extractor-frontend.dev/property?id=JP000004 i would to work with this http://extractor-frontend.dev/property/JP000004

this is my urlManager in config/web.php

urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,

        'rules' => [
            [
                'class'=>'yii\rest\UrlRule',
                'pluralize' => false,
                'controller' => 'property',

                'tokens' => [
                    '{id}' => '<id:\\w+>'
                ],
                'extraPatterns' => ['GET,HEAD property/{id}' => 'index',]

            ]
        ],
    ],

this is my .htaccess in web

RewriteEngine on
Options Indexes
Require all granted
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

if put 'enableStrictParsing' => false,

http://extractor-frontend.dev/site/about work fine ... rewrite rules works!


Solution

  • I use below code for my yii2 apps. I think your config method is eligible for yii1. It is recommended to use yii2 config method.

     'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'myaction/<id>/<param2>' => 'site/myaction',
    
                [
                    'pattern' => '<action>',
                    'route' => 'site/<action>',
                    'defaults' => ['action' => 'index']
                ],
            ]
        ]