Yii2的BackOffice与REST API的实现 [英] Yii2 BackOffice with API REST Implementation

查看:162
本文介绍了Yii2的BackOffice与REST API的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了Yii2一个应用程序,是作为一个旅行社一个后台。

I have developed an app in Yii2 that is as a backoffice for a Travel Agency.

我已经使用了基本的启动模板,用于GII(code生成器)创建CRUDS为模特(酒店,酒店客房,HotelImages等)

I have used the basic start template, and used gii (code generator) to create the CRUDS for the models (Hotels, HotelRooms, HotelImages, etc)

我也想创建两个不同的前端应用程序(一个用于零售,另一个用于其他机构),我想将其从后端(也许是angular.js应用程序)分开,打通REST的信息可能是一个不错的办法。

I also want to create two different front-end applications (one for retail, and the other for other agencies), and I thought to separate it from the backend (maybe an angular.js app) and get the info through REST could be a good approach.

所以我想使用的API基于REST与GII作出的车型。

So I want to use the API Rest based on the models made with gii.

我怎样才能做到这一点?
我已阅读本:
http://www.yiiframework.com/doc-2.0/guide -rest-快速的start.html

How can I achieve this? I have read this: http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

和它说,我必须切换控制器。

and It says that I have to switch the controllers.

目前我已经(与GII生成)

Currently I have (generated with gii)

类HotelController扩展控制器

和阅读引导它说,我一定要使用

and reading the guide it says that I have to use

类HotelController扩展ActiveController

但如果我有这样的改变,后台不工作了。

but if I make this change, the backoffice does not work anymore.

什么是最好的办法?

另一个 APIHotelController扩展ActiveController?

Make another APIHotelController that extends ActiveController?

我可以合并(以某种方式)与GII生成器?

Can I merge (in some sort of way) with the gii generated controller?

任何其他的方式来实现这种分离层(背前)?

Any other way to achieve this separation of layers (back-front) ?

谢谢!

推荐答案

最优雅的解决方案,我知道,到目前为止建立一个REST API Yii2 Web应用程序,而不与存在code搞乱或路由的配置是通过建设一个REST API为包含本身MVC元素一个独立的子应用程序,这意味着在Yii中的世界:作为一个的模块

The most elegant solution I know so far to build a REST API Yii2 web app without messing up with the existent code or the routes configurations is by building a REST API as a separate sub-application containing MVC elements by itself, which means in Yii's world : as a module.

您已经有一个工作code时,网​​站文件夹中访问,它有它自己的输入脚本的的index.php 文件中的,它自己的服务器配置(的.htaccess 如果使用文件的阿帕奇的),并在其自己的应用程序配置中的配置为您的应用将取决于之前分析您的网址和渲染你的视图的与其相关的控制器内的文件模式

You already have a working code, accessible within the web folder, it has it's own Entry Scripts within the index.php file, its own server configurations (.htaccess file if using apache) and its own app configurations within the config folder to which your app will depend before parsing your URL's and rendering your view files within their related controllers and models.

这样的想法是创建一个新的网​​站文件夹,可以把它叫做 API 例如,用自己输入脚本文件,自己的服务器配置,自己的配置文件夹和自己的控制器延伸的 ActiveController 类。

So the idea is to create a new web folder, lets call it api for example, with its own Entry Scripts file, own server configurations, own config folder and own controllers extending the ActiveController class.

那么这两个,你的网​​站应用程序和 API 服务将共享相同的模式文件验证,存储或检索数据。

Then both, your web app and api service will share the same models files to validate, store or retrieve data.

您的应用程序将从这个基本的基于模板的结构移动:

Your app will move from this basic template based structure :

+ assets
+ config
+ controllers
+ models
+ views
+ web
...

要这种新的结构(从下面链接的教程的):

To this new structure (from the tutorial linked below) :

+ web
+ config
+ controllers
...
+ api
  + config
  + modules
    + v1
      + controllers
  .htaccess
  index.php

您将需要再注册您的 API 服务作为独立的模块,以工作,被Yii被初始化。

You will need then to register your api service as separate module in order to work and being initialized by Yii.

这时你会使用不同的网址去你的网​​站应用程序和相关的的意见或休息 API 和相关的 JSON / XML 的内部 HTTP输出分别为:// [your_path] / WEB /控制器/动作的http:// [your_path] / API / V1 /控制器/动作 URL结构取决于你的网络和API CONFIGS

Then you will use different URLs to get to your web app and related views or Rest api and related json/xml outputs respectively within http://[your_path]/web/controller/action or http://[your_path]/api/v1/controller/action (url structure depends on your web and api configs).

为了实施必要的code,这里是一个伟大的<一个href=\"https://www.diggin-data.de/dd-cms/blog/post/view/id/1004/name/Creating+a+REST+API+for+Yii2-basic-template\"相对=nofollow>教程一步一步。它采用的基本模板结构如下警予的 API versionning 办法(的 V1 文件夹的)。

In order to implement the necessary code, here is a great step by step tutorial. It uses the basic template structure and follows yii's api versionning approach (the v1 folder).

这篇关于Yii2的BackOffice与REST API的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆