-1

1.how make this url

http://localhost/travia-api/backend/web/v1/flight?id=4FR996IN2F829M

to this

http://localhost/travia-api/backend/web/v1/flight/4FR996IN2F829M

this is my config

  'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'showScriptName' => false,
        'enablePrettyUrl' => true,
        'rules' => [
            'class' => 'yii\rest\UrlRule',
            'controller' => 'flight',

            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>/view',
            '<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>',
           '<controller:[\w\-]+>/<action:[\w\-]+>' => '<controller>/<action>',
           'v1/<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => 'v1/<controller>/<action>',
           'v1/<controller:[\w\-]+>/<action:[\w\-]+>' => 'v1/<controller>/<action>',
           'module/<module:[\w\-]+>/<controller:[\w\-]+>/<action:[\w\-]+>' => '<module>/<controller>/<action>',
        ],

and this is my htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

2.and when i want to add new pattern i add these line to config

extraPatterns[
   'GET search' => 'search'
]

after that i have error like these

Unknown Property – yii\base\UnknownPropertyException Setting unknown property: yii\web\UrlRule::GET search

i did a lot but i couldn't found something useful! thanks in advance!

GAMITG
  • 3,810
  • 7
  • 32
  • 51
Mohsen
  • 1,295
  • 1
  • 15
  • 45

1 Answers1

0

With this rules:

'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>/view',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>',

You only gets digits on theid try to get words too:

'<controller:\w+>/<id:\d+\w+>' => '<controller>/view',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+\w+>' => '<controller>/<action>/view',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+\w+>' => '<controller>/<action>',

Because you want digits and words:

http://localhost/travia-api/backend/web/v1/flight/4FR996IN2F829M

I dont know how to resolve the second question properly but try this:

extraPatterns[
   'GET{id}' => 'search'
]

I saw it here

EDIT:

Change this rules:

'v1/<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => 'v1/<controller>/<action>',
'v1/<controller:[\w\-]+>/<action:[\w\-]+>' => 'v1/<controller>/<action>',

For this:

'v1/flight/<id:[\d+\w+]>' => 'v1/flight/actionName',
'v1/flight/<action:\w+' => 'v1/flight/<action>',

And setit as a first rules. Remember to change actionName

Sageth
  • 1,102
  • 1
  • 15
  • 30