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

ctf0 picture ctf0 · Nov 24, 2014 · Viewed 16.5k times · Source

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

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

From what I understood is that I can make an app with either langs JS or 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.

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

Answer

Michael Coleman picture Michael Coleman · Nov 24, 2014

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

  1. When/Why learn/use angular in a wep project
  2. how to setup the folder structure

For setting up the folder structure, check out these resources

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.

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

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 integrated suite of solutions and to a wide range of issues, on top of an architecture that seems to work well.

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.

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.