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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程