dplm47571 2018-03-18 06:48
浏览 49
已采纳

除GET外,Yii2 REST URL无法正常工作

I tried to create a REST API by referring Yii2 REST GUIDE but unfortunately I got only GET method working.

Example URL:

http://dev.exp-yii.com/employee

apart from the above URL everything else gives me a NOT FOUND (404) error page (Not even a JSON response).

app\controllers\EmployeeController.php

<?php

namespace app\controllers;

use yiiest\ActiveController;

class EmployeeController extends ActiveController
{
    public $modelClass = 'app\models\Employee';

    /**
     * @return array
     */
    protected function verbs()
    {

        return [
            'index' => ['GET', 'HEAD'],
            'view' => ['GET', 'HEAD'],
            'create' => ['POST'],
            'update' => ['PUT', 'PATCH'],
            'delete' => ['DELETE'],
        ];

    }

}

app\models\Employee.php

<?php

namespace app\models;

use Yii;


class Employee extends \yii\db\ActiveRecord
{
    public $primaryKey = 'emp_no';

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'employees';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['emp_no', 'birth_date', 'first_name', 'last_name', 'gender', 'hire_date'], 'required'],
            [['emp_no'], 'integer'],
            [['birth_date', 'hire_date'], 'safe'],
            [['gender'], 'string'],
            [['first_name'], 'string', 'max' => 14],
            [['last_name'], 'string', 'max' => 16],
            [['emp_no'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'emp_no' => 'Emp No',
            'birth_date' => 'Birth Date',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'gender' => 'Gender',
            'hire_date' => 'Hire Date',
        ];
    }

web.php Configuration

'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ]

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'rules' => [
                ['class' => 'yiiest\UrlRule', 'controller' => 'employer'],
            ],
        ],

.htaccess

RewriteEngine on

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

I hope that I have provided every relevant information to solve my problem. Thanks in advance. :-)

  • 写回答

1条回答 默认 最新

  • duandu6497 2018-03-18 09:03
    关注

    You have defined the name of the controller as employer whereas it should be employee if i am not wrong and that is not a typo writing the code here

    Change to the following

     ['class' => 'yiiest\UrlRule', 'controller' => 'employee'],
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号