AngularJS 和 Laravel 使用什么应用程序结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18420069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What application structure to use with AngularJS and Laravel?
提问by
I recently started to build a large social network, and and I thought my structure was good but it turned out I built this logic up badly.
我最近开始建立一个大型社交网络,我认为我的结构很好,但事实证明我建立的这个逻辑很糟糕。
I mixed my views with AngularJS (bad idea), skipped blade extension, but since I'm using a lot of block and sidebar includes it became a pain in the butt.
我将我的观点与 AngularJS(坏主意)混合在一起,跳过了刀片扩展,但由于我使用了大量的块和侧边栏包含它变得很痛苦。
Currently I am just handling form validations with angular, but actually all of my site pages will require ajax, data pulling, etc.
目前我只是用 angular 处理表单验证,但实际上我所有的网站页面都需要 ajax、数据拉取等。
I was searching around the net and I saw that angular views are stored in the public folder, but since all my pages will use angular is it a good idea to store all my views in the public, and just use Laravel as a back end?
我在网上搜索,我看到 angular 视图存储在 public 文件夹中,但是由于我的所有页面都将使用 angular,将我的所有视图存储在 public 中是一个好主意,并且只使用 Laravel 作为后端?
I know this is a silly question but I'm confused a bit.
我知道这是一个愚蠢的问题,但我有点困惑。
Any help hint appreciated.
任何帮助提示表示赞赏。
采纳答案by Maximilian Hoffmann
There are two ways to combine these frameworks:
有两种方法可以组合这些框架:
Only client-side rendering
This is the easier way and used by most web applications. In this case you would use Laravel as an API endpoint which will return JSON. Angular can query this data through its
$http
or$resource
service and compile the templates, which you store in the public folder. Angular templates are just HTML with directives and some {{var}} statements. This way Angular does all the routing too.Server-side and client-side rendering
This is the harder way where Laravel would do the routing and compile some templates on the server-side. You would use Angular only for some interactions on the site in a way you would use jQuery for example. The benefit of this approach is performance as users will get the full HTML the first time they visit your site. The disadvantage is that you may have to write some logic twice and can't use some of Angular's features.
仅客户端渲染
这是更简单的方法,并且被大多数 Web 应用程序使用。在这种情况下,您将使用 Laravel 作为将返回 JSON 的 API 端点。Angular 可以通过它的
$http
或$resource
服务查询这些数据,并编译你存储在公共文件夹中的模板。Angular 模板只是带有指令和一些 {{var}} 语句的 HTML。这样 Angular 也完成了所有的路由。服务器端和客户端渲染
这是 Laravel 进行路由并在服务器端编译一些模板的更难的方式。例如,您将仅将 Angular 用于网站上的某些交互,就像使用 jQuery 一样。这种方法的好处是性能,因为用户将在第一次访问您的网站时获得完整的 HTML。缺点是你可能要写一些逻辑两次,不能使用 Angular 的一些功能。
回答by Sergiu Paraschiv
To actually benefit from most of angular's features you should write a Single Page Application. This means you will communicate with the server through web APIs and you won't have anyLaravel server-side templates.
要真正从 angular 的大部分功能中受益,您应该编写一个单页应用程序。这意味着您将通过 Web API 与服务器通信,并且您将没有任何Laravel 服务器端模板。
So yes, you should write two decoupled applications. One client-side, using Angular and one server-side that exposes a web API, preferably RESTful.
所以是的,您应该编写两个解耦的应用程序。一个客户端,使用 Angular,一个服务器端公开 Web API,最好是 RESTful。
This way you could switch from JS/HTML/CSS on the client side to Flash or Silverlight or something else and from Laravel/PHP/MySQL to .NET or NodeJS or Meteor/MongoDB.
这样你就可以从客户端的 JS/HTML/CSS 切换到 Flash 或 Silverlight 或其他东西,从 Laravel/PHP/MySQL 切换到 .NET 或 NodeJS 或 Meteor/MongoDB。
回答by Peter Drinnan
Sergiu is correct, but in some cases Laravel still offers benefits that cannot be achieved with client-side templates. This is related to SEO and WCAG (accessibility).
Sergiu 是对的,但在某些情况下,Laravel 仍然提供客户端模板无法实现的好处。这与 SEO 和 WCAG(可访问性)有关。
AngularJS renders content by way of DOM manipulation so search engines cannot determine what content is shown after those manipulations are complete. This is also the case for screen readers. For this reason some content must be delivered by way of server-side view constructs. That is why Wordpress and Laravel have long and healthy futures.
AngularJS 通过 DOM 操作呈现内容,因此搜索引擎无法确定在这些操作完成后显示什么内容。屏幕阅读器也是如此。出于这个原因,某些内容必须通过服务器端视图构造来传递。这就是 Wordpress 和 Laravel 拥有长期而健康的未来的原因。
On the back-end or in cases where SEO and WCAG are not important, data binding client side templates such as those used with AngularJS and Ember will be used increasingly as more developers learn how to use them.
在后端或在 SEO 和 WCAG 不重要的情况下,随着越来越多的开发人员学习如何使用它们,将越来越多地使用数据绑定客户端模板,例如与 AngularJS 和 Ember 一起使用的模板。
In terms of whether to use AngularJS or Laravel for view constructs it would be best to learn how to use both and apply where most appropriate.
关于是使用 AngularJS 还是 Laravel 进行视图构造,最好学习如何使用两者并在最合适的地方应用。