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

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

问题描述

我在 Yii2 中开发了一个应用程序,作为旅行社的后台.

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

我使用了基本的启动模板,并使用 gii(代码生成器)为模型(Hotels、HotelRooms、HotelImages 等)创建 CRUDS

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.

所以我想使用基于gii制作的模型的API Rest.

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

我怎样才能做到这一点?我读过这个:http://www.yiiframework.com/doc-2.0/guide-rest-quick-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 扩展控制器

阅读指南说我必须使用

HotelController 类扩展 ActiveController

但如果我进行此更改,后台将不再工作.

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

最好的方法是什么?

制作另一个扩展 ActiveController 的 APIHotelController?

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 应用程序而不弄乱现有代码或路由配置的最优雅的解决方案是构建一个 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.

你已经有一个工作代码,可以在 web 文件夹中访问,它有它自己的 Entry Scriptsindex.php 文件中,它自己的服务器配置(.htaccess 文件,如果使用 apache) 和它自己的应用程序配置在 config 文件夹中,您的应用程序将依赖该文件夹,然后解析您的 URL 并在其相关 view 文件中呈现您的文件代码>控制器和模型.

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.

所以想法是创建一个新的 web 文件夹,我们称之为 api 例如,有自己的 Entry Scripts 文件,自己的服务器配置,自己的 config 文件夹和自己的 控制器 扩展了 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.

然后,您的 web 应用程序和 api 服务将共享相同的 models 文件来验证、存储或检索数据.

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
...

到这个新结构(来自下面链接的教程):

+ 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.

然后您将使用不同的 URL 来访问您的 web 应用程序和相关的 views 或 Rest api 和相关的 json/xml 分别在 http://[your_path]/web/controller/actionhttp://[your_path]/api/v1/controller/action 中输出(url 结构取决于您的 web 和 api 配置).

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).

为了实现必要的代码,这里有一个很棒的 分步教程.它使用基本的模板结构并遵循 yii 的 api 版本控制 方法(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 与 API REST 实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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