如果 Laravel 已经用作后端,为什么还要在前端使用 AngularJs?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27098987/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:27:45  来源:igfitidea点击:

Why use AngularJs in frontend if Laravel is already used as backend?

phpangularjslaravelsingle-page-application

提问by ctf0

I know that AngularJs is FrontEnd and Laravel is Backend but my questions is:

我知道 AngularJs 是前端,而 Laravel 是后端,但我的问题是:

Why do I need to use AngularJs in the first place where everything I need is done through Laravel ?

为什么我首先需要使用 AngularJs,我需要的一切都是通过 Laravel 完成的?

From what I understood is that I can make an app with either langs JSor PHP, but I find lots of users use both which for me as a beginner it seems like an overkill, so could someone please explain in the simplest possible form of an answer.

据我了解,我可以使用 langs JSPHP制作应用程序,但我发现很多用户都同时使用这两者,这对我作为初学者来说似乎有点矫枉过正,所以有人可以用最简单的形式解释一下回答。

What is the benefit of using both in the same project ? And how do you usually structure your app/folderswhen you use both specially that both are MVC ?

在同一个项目中使用两者有什么好处?app/folders当你特别使用两者都是 MVC 时,你通常如何构建你的结构?

采纳答案by Michael Coleman

I suspect the question may conflict with stackoverflow's quality guidelines and you may almost be better asking two separate questions. i.e.

我怀疑这个问题可能与 stackoverflow 的质量指南相冲突,你可能最好问两个单独的问题。IE

  1. When/Why learn/use angular in a wep project
  2. how to setup the folder structure
  1. 何时/为什么在 wep 项目中学习/使用 angular
  2. 如何设置文件夹结构

For setting up the folder structure, check out these resources

要设置文件夹结构,请查看这些资源

http://scotch.io/tutorials/php/create-a-laravel-and-angular-single-page-comment-applicationhttps://www.youtube.com/watch?v=hqAyiqUs93c

http://scotch.io/tutorials/php/create-a-laravel-and-angular-single-page-comment-application https://www.youtube.com/watch?v=hqAyiqUs93c

I can only really speak to why I use laravel with angularjs and you are right that you can use laravel without angular, but the reason I use angular as well as laravel is because there are some things that javascript is just better positioned for than server side apps e.g.

我只能说为什么我将 laravel 与 angularjs 一起使用,您可以在没有 angular 的情况下使用 laravel 是对的,但是我使用 angular 和 laravel 的原因是因为有些事情 javascript 比服务器端更适合定位应用程序例如

  • Using ajax and not having to reload the page whenever you want to check/process some small thing, like whether an email address a user types into a form is valid or not.
  • its faster because when you ajax back to the server, you dont have to load the entire page, just the pieces that are needed.
  • You can give immediate feedback to the user - e.g. when they are filling out a form - you can check that their input validates and give them feedback immediately. With lavavel you have to perform a round trip back to the server and possibly return a collection of error messages.
  • 使用 ajax 而不必在您想要检查/处理一些小事情时重新加载页面,例如用户在表单中输入的电子邮件地址是否有效。
  • 它更快,因为当您将 ajax 返回到服务器时,您不必加载整个页面,只需加载所需的部分。
  • 您可以立即向用户提供反馈——例如,当他们填写表单时——您可以检查他们的输入是否有效并立即向他们提供反馈。使用 lavavel,您必须执行返回服务器的往返行程,并可能返回一组错误消息。

So if you want your website/webapp to have those benefits you need to write the functionality in javascript. I can't say I have used all the frameworks to then say that angular is the best one, but for me angular provides an integratedsuite of solutions and to a wide range of issues, on top of an architecture that seems to work well.

因此,如果您希望您的网站/网络应用程序具有这些好处,您需要在 javascript 中编写功能。我不能说我已经使用了所有框架然后说 angular 是最好的,但对我来说,angular 提供了一套集成的解决方案和广泛的问题,在一个似乎运行良好的架构之上。

I think this is partly why there are quite a few new concepts to learn and the need for these components is not immediately obvious.
This may be due to the fact that a component you are using to perform some function, e.g. using a $scope object to relay data between a controller and a view, also serves a function in the bigger picture, e.g. maintaining separation between the view and the controller - which helps with organising code, testability as well as other factors.

我认为这就是为什么有很多新概念需要学习并且对这些组件的需求不是很明显的部分原因。
这可能是因为您用来执行某些功能的组件(例如使用 $scope 对象在控制器和视图之间中继数据)也在更大范围内提供功能,例如保持视图和视图之间的分离。控制器 - 有助于组织代码、可测试性以及其他因素。

If you use angular as well as Laravel (as opposed to just Laravel by itself) you will then have the option of giving your users these benefits, because you have more options available e.g. use laravel where it best suits the requirements of your app and users or use angular where it gives the best result - and you will have a better web app in the end.

如果您同时使用 angular 和 Laravel(而不是单独使用 Laravel),那么您将可以选择为您的用户提供这些好处,因为您有更多可用的选择,例如在最适合您的应用程序和用户要求的地方使用 Laravel或者在可以提供最佳结果的地方使用 angular - 最终您将拥有更好的 Web 应用程序。

回答by chelanga dennis

The big reason why i use laravel for backend and angularjs for frontend is that it make the app to be restful (both backend and frontend). The backend would only produce and consume data in json form making tasks like creating a mobile app version of the application an easier job mainly because of json.

我将 laravel 用于后端,将 angularjs 用于前端的一个重要原因是它使应用程序变得安静(后端和前端)。后端只会以 json 形式生成和使用数据,这使得创建应用程序的移动应用程序版本等任务变得更容易,主要是因为 json。