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

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

问题描述

我有两个的Yii &NBSP经验; 2和的 AngularJS ,而是分开。我有两个问题:

是否有可能使用的Yii&NBSP AngularJS; 2的看法?我问的可能的而不是可行性的,因为我觉得这个问题可以在路由到了。

此外,它是很公平(性能)使用Yii的  2和AngularJS在一起吗? (两者都是如此MVC模块化,管理code)

我搜索了很久,但我无法找到任何合适的资源。什么是解释!


解决方案

,您可以使用 AngularJS中的的Yii&NBSP 2 意见后,实施不同生根方法。这里是的教程入手

,我不建议这样做(虽然两者的的Yii&NBSP; 2 AngularJS 与原生支持大框架< A HREF =htt​​p://en.wikipedia.org/wiki/Re$p$psentational_State_Transfer相对=nofollow> REST )。所以,正确的方法是使用 AngularJS 以建立您的前端的和使用的Yii&NBSP; 2 只需提供的服务器API 。这里是一个很好的结构这样做的:


  

从@AlekseiAkireikin结构<一href=\"http://stackoverflow.com/questions/28744215/yii2-directory-structure-with-angular-js#answer-28749404\">this堆栈&NBSP;溢出后


 项目/
     后端/ // Yii的应用2
       网络/ //公众可见的后端文件夹
         index.php文件//入口点
       配置/
       控制器/
       楷模/
       ...
     前端/
       应用程序/ //你的AngularJS应用在这里
         CSS / //样式(.LESS或的CSS)
         IMG / //图片
         LIB / //第三方库,如jQuery的或AngularJS
         JS / // .js文件(控制器,服务等)
         谐音/ //模板(的.html)
         的index.html
       测试/ // AngularJS测试
       node_modules /
       ...

借助的Yii的RESTful API框架将提供它可以与您的内置的 AngularJS 应用程序或者沟通干净 API 未来的移动应用程序,甚至提供的的资源和/或服务的到其他网站或软件。如果你关心性能,然后用两个去使用REST。结构良好的RESTful应用程序是伟大的轻松构建一个良好的缓存的系统背后灵活的策略。你甚至可以举办一个服务器上的后端和数据库(如 Amazon EC2上)只提供的 JSON(和/或XML)对最小带宽使用的数据,并且具有存储在一个优化的的前端CDN (如Amazon S3或者其他CDN提供商)以更低的成本和更快的答案。

下面是两个例子实施AngularJS和Yii的&NBSP; 2 REST中:并的这个

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

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.

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, you can use AngularJS in Yii 2 views after implementing a different rooting approach. Here is a tutorial to start with.

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 from this Stack Overflow post

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

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.

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

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

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