First, you need to add a. htaccess file under the basic/web/folder
This entry will automatically access the index.php file, the URL will not be garbled
<mod_rewrite.c>Options +followsymlinksindexignore */*rewriteengine on# If a directory or a file exists, use it directlyrewritecond%{request_filename}!-frewritecond%{request_filename}!-d# OT Herwise forward it to index.phprewriterule. index.php</ifmodule>
Let's take a look at what's in the official documentation.
First, create a Yii\db\activerecord classapp\models\User来访问user表
Then, create a controller class app\controllers\UserController
as follows,
<? phpnamespace app\controllers; Use Yii\rest\activecontroller; class extends activecontroller{ public$modelClass = ' App\models\user ';}
Then, modify the configuration of the urlManager
component in your application configuration:
' Urlmanager ' => [ ' enableprettyurl ' = true , ' enablestrictparsing ' = = true , ' Showscriptname ' = false , ' rules ' => [ [ ' class ' = ' Yii\rest\urlrule ', ' controller ' = ' user '],, ]
// used to indicate whether Urlmanager is enabled for URL beautification, known as the path format URL in Yii1.1, or renamed in Yii2.0. //not enabled by default. But in the actual use, especially the product environment, generally will be enabled. public $enablePrettyUrlfalse; // whether to enable strict parsing, such as enabling strict parsing, requires that the current request should match at least 1 routing rules, //Otherwise it is considered an invalid route. //This option is only valid if Enableprettyurl is enabled. public $enableStrictParsingfalse;
// Specifies whether the URL is in the reserved entry script index.php Public $showScriptName true;
After changing to the above settings, Http://localhost/basic/web/users can access the API directly (note that it automatically adds a s for you)
Now let's take a look at a urlmanager configuration rule:
' Urlmanager ' = [ ' Enableprettyurl ' =true,//true to open beautify' Enablestrictparsing ' =true,//true to enable strict parsing, requires that the current request should match at least 1 routing rules, not to route 404, for example http://localhost/basic/web/will 404;false is to find the index' Showscriptname ' =false,//Specifies whether the URL is in the reserved entry script index.php' Rules ' = [ [' Class ' = ' Yii\rest\urlrule ', ' controller ' = ' user ', ' pluralize ' =true],//true requires access to users, false requires access to user, default is True], ],
GET /users
: List all users on a per-page basis
HEAD /users
: Displays summary information for the user list
POST /users
: Create a new user
GET /users/123
: Returns the details of user 123
HEAD /users/123
: Displays overview information for user 123
PATCH /users/123
and PUT /users/123
: Update user 123
DELETE /users/123
: Delete User 123
OPTIONS /users
: Displays verbs about /users
the end support
OPTIONS /users/123
: Displays verbs about /users/123
the end support
Questions about the Yii2 REST API