php - Yii2 controller or configuration raise that only handle the following request methods: GET, HEAD -


i trying simple rest response using json format.

i created simple model:

model

<?php  namespace app\models;  use yii; use yii\db\activerecord;  class post extends activerecord {             /**      * @inheritdoc      */      public static function tablename()     {         return 'posts';     }      public function rules()     {         return [             [['title', 'content'], 'required'],          ];     } } 

and simple controller:

controller

namespace app\controllers;  use yii; use yii\rest\activecontroller;  class postscontroller extends activecontroller {            public $modelclass = "app\models\post"; }  ?> 

it configuration of web.php file:

  $config = [     'id' => 'basic',     'basepath' => dirname(__dir__),     'bootstrap' => ['log'],     'components' => [         'request' => [             'cookievalidationkey' => 'wchybrv4bxfljynwzrpjz_5varaaee9u',             'parsers' => [                 'application/json' => 'yii\web\jsonparser',             ],         ],         'urlmanager' => [             'class' => 'yii\web\urlmanager',             'enableprettyurl' => true,             'enablestrictparsing' => false,             //'showscriptname' => false,             'rules' => [                 'class' => 'yii\rest\urlrule',                 'controller' => 'posts',                        ]         ],         other attributes... 

the important part (i think so) urlmanager configuration, maybe missing don't know error below.

the error

when execute request method get, ok, other method fail. error:

{   "name": "method not allowed",   "message": "method not allowed. url can handle following request methods: get, head.",   "code": 0,   "status": 405,   "type": "yii\web\methodnotallowedhttpexception" } 

i want simple response guy got in this video did same configuration, step step.

thank you.

update

there more information, when using postman, got headers result.

enter image description here

i don't understand why allow -> get, head

it body result:

enter image description here

try configuration:

'urlmanager' => [     'enableprettyurl' => true,     'enablestrictparsing' => true,     'showscriptname' => false,     'rules' => [         ['class' => 'yii\rest\urlrule', 'controller' => 'post'],     ], ], 

see config , full list of available requests in official docs.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -