Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the pitfalls that should be paid attention to in Yii2's implementation of RESTful-style API?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about Yii2 to achieve RESTful-style API which should pay attention to which pit, the editor thinks it is very practical, so share it with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

The process for Yii2 to implement RESTful-style API is as follows:

1. Create a new folder for the same level directories of WEB front-end (frontend) and back-end (backend), and name the files in api,api to completely copy the original backend files.

2. The common\ config\ bootstrap.php file needs to be modified to add an alias alias to the newly created application.

Yii::setAlias ('@ api', dirname (dirname (DIR)). '/ api')

3. Make sure your web server turns on rewrite rules! Configure apache or nginx! Here, please google or Baidu.

In nginx, add to the relevant location in vhost.conf or nginx.conf in the conf folder:

Location / {

Try_files $uri $uri/ / index.php$is_args$args

}

In apache, add .htaccess to the corresponding api/web (the server directory pointed to by the domain name):

RewriteEngine on

RewriteCond% {REQUEST_FILENAME}!-d

RewriteCond% {REQUEST_FILENAME}!-f

RewriteRule. Index.php

4. Api application beautifies routing (pay attention to the brackets, the case of the name, the class name is used to hump, but the controller name corresponding to controller must be lowercase)

Then configure the api/config/main.php file

'urlManager' = > [

'enablePrettyUrl' = > true

'showScriptName' = > false

'enableStrictParsing' = > true

'rules' = > [[' class'= > 'yii\ rest\ UrlRule','controller'= >' article'],]

If you are worried about affecting other access addresses (such as gii cannot connect), you can comment 'enableStrictParsing' = > true first, and then turn it on after passing it to the server (to increase security)

5. When you are ready, start writing conroller and model:

Create a new file Article.php in the www root directory-- > common-- > models (you can use Gii)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report