单个应用程序中的 Yii2 + AngularJS - 如何? [英] Yii2 + AngularJS in a single application - how?

查看:30
本文介绍了单个应用程序中的 Yii2 + AngularJS - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用 Yii 2 和 AngularJS,但分开.我有两个问题:

I have experience with both Yii 2 and AngularJS, but separately. I have two questions:

是否可以在 Yii 2 的视图中使用 AngularJS?我问的是可能而不是可行,因为我认为问题可能出在路由上.

Is it possible to use AngularJS in Yii 2's view? I am asking possible instead of feasible, because I think the problem may have arrived at routing.

另外,一起使用 Yii 2 和 AngularJS 是否足够公平(为了性能)?(两者都是 MVC,所以对于模块化、可管理的代码.)

Also, is it fair enough (for performance) to use Yii 2 and AngularJS together? (Both are MVC so for modular, manageable code.)

我搜索了很长时间,但找不到任何合适的资源.什么解释!

I searched for a long time, but I was unable to find any proper resource. What is the explanation!

推荐答案

YES,你可以在 Yii 2 视图中使用 AngularJS 之后实施不同的生根方法.这是入门教程.

YES, you can use AngularJS in Yii 2 views after implementing a different rooting approach. Here is a tutorial to start with.

但是,我不建议这样做(虽然 Yii 2AngularJS 都是很好的框架,原生支持 REST).所以正确的方法是使用 AngularJS 来构建你的 frontend 并使用 Yii 2 来提供 服务器 API.这是一个很好的结构:

But NO, I don't recommend doing so (while both Yii 2 and AngularJS are great frameworks with native support of REST). So the proper way is to use AngularJS to build your frontend and use Yii 2 just to provide a server API. Here is a good structure to do so:

Structure by @AlekseiAkireikin 来自 这篇 Stack Overflow 帖子

   project/
     backend/        // Yii 2 application
       web/          // Public visible backend folder
         index.php   // Entry point
       config/
       controllers/
       models/
       ...
     frontend/
       app/          // Your AngularJS application here
         css/        // Styles (.less or .css)
         img/        // Images
         lib/        // Third-party libraries such as jQuery or AngularJS
         js/         // .js files (controllers, services, etc.)
         partials/   // Templates (.html)
         index.html
       tests/        // AngularJS tests
       node_modules/
       ...

Yii RESTful API 框架 将提供一个干净的 API 可以与您的内置 AngularJS 应用程序或也许进行通信未来的移动应用程序,甚至为其他网站或软件提供资源和/或服务.如果您关心性能,请同时使用两者并使用 REST.一个结构良好的 RESTful 应用程序非常适合轻松构建一个良好的缓存系统,背后有一个灵活的策略.您甚至可以在仅提供 JSON(和/或 XML) 数据用于最小带宽使用,并将您的前端存储在优化的 CDN(如 Amazon S3 或其他 CDN 提供商),成本更低,响应速度更快.

The Yii RESTful API framework will provide a clean API which can communicate with your built-in AngularJS application or maybe a future mobile app or even providing resources and/or services to other websites or software. If you care about performance then go with both and use REST. A well-structured RESTful application is great to easily build a good caching system with a flexible strategy behind. You can even host your backend and database on a server (like Amazon EC2) providing only JSON (and/or XML) data for minimum bandwidth use, and having your frontend stored on an optimized CDN (like Amazon S3 or other CDN provider) with lower cost and faster answers.

以下是在 REST 中实现 AngularJS 和 Yii 2 的两个示例:this这个.

Here are two examples implementing AngularJS and Yii 2 within REST: this and this.

这篇关于单个应用程序中的 Yii2 + AngularJS - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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