作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为yii2基本模板创建REST API。我遵循以下link。
我创建了一个名为users
的表,一个名为UserController
的 Controller
<?php
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}
?>
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '4534',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
最佳答案
使用这些配置:
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
http://localhost/~user/restapi/web/users
http://localhost/~user/restapi/web/users/1
yii\rest\UrlRule::$pluralize
属性配置为不这样做,否则Yii会自动将用于端点的 Controller 名称进行复数。 .htaccess
文件添加到
web
文件夹中来配置服务器(请使用下面的链接(如果使用nginx):
# Set document root to be "basic/web"
DocumentRoot "path/to/basic/web"
<Directory "path/to/basic/web">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
关于rest - 如何为Yii2-basic-template创建REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32626212/
我是一名优秀的程序员,十分优秀!