首先,需要在basic/web/文件夹下添加一个.htaccess文件

这样进入项目就会自动访问index.php文件,url就不会错乱了

<IfModule mod_rewrite.c>

Options +FollowSymLinks

IndexIgnore */*

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

</IfModule>

接下来详解一下官方文档里的东西

首先,创建一个 yii\db\ActiveRecord 类app\models\User来访问user表

然后,创建一个控制器类 app\controllers\UserController 如下,

<?php

namespace app\controllers;

use yii\rest\ActiveController;

class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}

Then, modify the configuration about the urlManager component in your application configuration:

'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
]
    // 用于表明urlManager是否启用URL美化功能,在Yii1.1中称为path格式URL,
// Yii2.0中改称美化。
// 默认不启用。但实际使用中,特别是产品环境,一般都会启用。
public $enablePrettyUrl = false; // 是否启用严格解析,如启用严格解析,要求当前请求应至少匹配1个路由规则,
// 否则认为是无效路由。
// 这个选项仅在 enablePrettyUrl 启用后才有效。
public $enableStrictParsing = false;
// 指定是否在URL在保留入口脚本 index.php
public $showScriptName = true;

改成上述设置后,http://localhost/basic/web/users 可以直接访问api(注意自动帮你加了个s)

现在来详解一下一个urlManager的配置规则:

        'urlManager' => [
'enablePrettyUrl' => true,//true 打开美化
'enableStrictParsing' => true,//true启用严格解析,要求当前请求应至少匹配1个路由规则,不给路由就404,例如http://localhost/basic/web/就会404;false就是自己找index
'showScriptName' => false,//指定是否在URL在保留入口脚本 index.php
'rules' => [
['class' => 'yii\rest\UrlRule',
'controller' => 'user',
'pluralize'=>true], //true需要访问users; false需要访问user,默认是true
],
],
  • GET /users: 逐页列出所有用户
  • HEAD /users: 显示用户列表的概要信息
  • POST /users: 创建一个新用户
  • GET /users/123: 返回用户 123 的详细信息
  • HEAD /users/123: 显示用户 123 的概述信息
  • PATCH /users/123 and PUT /users/123: 更新用户123
  • DELETE /users/123: 删除用户123
  • OPTIONS /users: 显示关于末端 /users 支持的动词
  • OPTIONS /users/123: 显示有关末端 /users/123 支持的动词

关于yii2 REST api 的问题的更多相关文章

  1. Yii2 Restful Api 401

    采用Yii2 Restful Api方式为APP提供数据,默认你已经做好了所有的编码和配置工作.采用Postman测试接口: 出现这个画面的一个可能原因是:access_token的写法有误,如果你使 ...

  2. Yii2 restful api创建,认证授权以及速率控制

    Yii2 restful api创建,认证授权以及速率控制 下面是对restful从创建到速率控制的一个详细流程介绍,里面的步骤以及截图尽可能详细,熟悉restful的盆友可能觉得过于繁琐,新手不妨耐 ...

  3. yii2 RESTful API 405 Method Not Allowed

    关于 Yii2 中 RESTful API 的开发,可以参考另一篇随笔 http://www.cnblogs.com/ganiks/p/yii2-restful-api-dev.html 测试的过程中 ...

  4. yii2 RESTful api的详细使用

    作者:白狼 出处:http://www.manks.top/yii2-restful-api.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...

  5. Yii2 Restful api设计--App接口编程

    Yii2框架写一套RESTful风格的API,对照魏曦教你学 一,入门 一.目录结构 实现一个简单地RESTful API只需用到三个文件.目录如下: frontend ├─ config │ └ m ...

  6. yii2 restful api——app接口编程实例

    <?php namespace common\components; use common\models\Cart; use common\models\User; use Yii; use y ...

  7. Yii2 Restful API 原理分析

    Yii2 有个很重要的特性是对 Restful API的默认支持, 通过短短的几个配置就可以实现简单的对现有Model的RESTful API 参考另一篇文章: http://www.cnblogs. ...

  8. yii2 RESTful API Develop

    参考文档:http://www.yiiframework.com/doc-2.0/guide-rest.html 以 DB 中的 news 表为例创建该资源的 RESTful API,最终的测试通过工 ...

  9. Yii2 components api/controller

    When we wrote API, those controllers need to implement the following feature: 1. return JSON format ...

随机推荐

  1. Mysql数据库导出sql脚本

    1. 运行环境Centos mysqldump -h localhost -u root -p etv > ./etv.sql etv 是要导出的数据库名 > 设置导出的路径和文件名

  2. Spring积累

    <tx:annotation-driven/>  (Spring的XML配置里两大Bean的声明之一) 那我们是否就可以在程序中所有被spring管理的类(@Controller.@Ser ...

  3. java+opencv+intellij idea实现人脸识别

    首先当然是需要安装opencv了,我用的是opencv2.4.13.下载完之后就可以直接安装了,安装过程也很简单,直接下一步下一步就好,我就不上图了. 接下来在opencv下找到jar包,比如我直接安 ...

  4. linux 安装unrar

    Centos 6 32位下安装 wget http://pkgs.repoforge.org/unrar/unrar-4.2.3-1.el6.rf.i686.rpmrpm -ivh unrar-4.2 ...

  5. Pandas稀疏数据

    当任何匹配特定值的数据(NaN/缺失值,尽管可以选择任何值)被省略时,稀疏对象被“压缩”. 一个特殊的SparseIndex对象跟踪数据被“稀疏”的地方. 这将在一个例子中更有意义. 所有的标准Pan ...

  6. spring3: 访问Resource — ResourceLoader/ResourceLoaderAware接口

    4.3.1  ResourceLoader接口 ResourceLoader接口用于返回Resource对象:其实现可以看作是一个生产Resource的工厂类. public interface Re ...

  7. Ajax-05 使用XMLHttpRequest和jQuery实现Ajax实例

    需求: (django)使用XMLHttpRequest和jQuery实现Ajax加法运算 url.py: from django.conf.urls import url from hello im ...

  8. lightoj1234调和级数+欧拉常数

    数据比较小时直接算,数据比较大时用公式1+1/2+...+1/n=inn+C+1/(2*n)   ,C是欧拉常数0.5772156649 刚开始用的这个1+1/2+...+1/n=in(n+1)+C, ...

  9. Codeforces Round #181 (Div. 2)C

    用lucas定理, p必须是素数 对于单独的C(n, m) mod p,已知C(n, m) mod p = n!/(m!(n - m)!) mod p.显然除法取模,这里要用到m!(n-m)!的逆元. ...

  10. Map类集合

    集合类                                        Key                            Value                     ...