Home » Php » php – What application structure to use with AngularJS and Laravel?

php – What application structure to use with AngularJS and Laravel?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

Currently I am just handling form validations with angular, but actually all of my site pages will require ajax, data pulling, etc.

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?

I know this is a silly question but I’m confused a bit.

Any help hint appreciated.

How to&Answers:

There are two ways to combine these frameworks:

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

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

Answer:

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 any Laravel server-side templates.

So yes, you should write two decoupled applications. One client-side, using Angular and one server-side that exposes a web API, preferably 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.

Answer:

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

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.

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.

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.