Yii2 URL Routing

I have a problem calling url of my rest api in Yii2. I want to call a url like:

http: //localhost/index-dev.php/myapi/collection/18

where 18 - Id.

In my web.php config, I have the following code, with different settings from other programmers:

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => true,
        'rules' => [
            ['class' => 'yii\rest\UrlRule', 'controller' => ['coding/nodes', 'coding/scales','myapi/collection']],                
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
        ],            
    ],

      

when i call my url i get

Not Found (# 404)

What am I doing wrong?

+3


source to share


3 answers


You need to use the plural name of your model class in the url to access the same model:

http: //localhost/index-dev.php/myapi/collections/18



Take a look at the yii \ rest \ UrlRule documentation :

The above code will create a whole set of URL rules that support the following RESTful API endpoints:

  • 'PUT, PATCH users /' => 'user / update': update user
  • 'DELETE users /' => 'user / delete': delete user
  • 'GET, HEAD users /' => 'user / view': return user details / view / parameters
  • 'POST users' => 'user / create': create a new user
  • 'GET, HEAD users' => 'user / index': return list / overview / parameters of users
  • 'users /' => 'user / options': process all unprocessed user verbs
  • 'users' => 'user / options': handle all raw verbs of the user collection
+4


source


I had the same problem, you can disable prural



'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => true,
    'rules' => [
        ['class' => 'yii\rest\UrlRule', 'controller' => ['coding/nodes', 'coding/scales','myapi/collection']],                
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        'pluralize' => false,
    ],            
],

      

+4


source


I suggest to create a separate module for the API and configure your UrlManager as ... :)

'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                [
                    'class' => 'yii\rest\UrlRule', 
                    'pluralize'=>false,
                    'controller' => ['v1/country','v1/user'],
                    'tokens' => [
                        '{id}' => '<id:\\w+>'
                    ],
                    'extraPatterns' => [
                        'POST register' => 'register', //from url 
                        'GET exists'=>'exists',
                        'POST login'=>'login',
                        'POST follow'=>'follow',
                        'POST category'=>'category',
                        'PUT profile'=>'profile',
                        'PUT change_password'=>'change_password',
                        'PUT feed_interested'=>'feed_interested',
                    ],


                ]
            ],        
        ]

      

More @ Create Rest Api

+2


source







All Articles