2

I'm creating a CRUD-based web project, and am thinking about using a server side MVC framework (such as CodeIgniter or Laravel) in tandem with a client side MVC framework (such as AngularJS). Is this a fairly common approach? After all, it's clear that I need something on the server, and that doing MVC on the client as well can enhance my application. But I feel that two MVC frameworks on the same project would just get in the way of each other and lead to disproportionate complexity and repetition.

What kind of approach or architecture would help me get the best out of both MVC frameworks when using one on the client, the other on the server?

closed as primarily opinion-based by Kilian Foth, ratchet freak, psr, World Engineer Feb 23 '15 at 23:58

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

8

Laravel and AngularJS actually work quite well together; there's a good video on Laracasts about it and a few good articles online.

In general you wouldn't want to use multiple MVC frameworks in one project as it increases bloat, as well as a lot of redundancy. Your average PHP MVC framework will include an ORM, Templating engine, router and so on - components that you only need one of. In fact, using two will most likely increase complexity and introduce conflicts.

The exception here is where frameworks are separated clearly, like in the case of a server side framework and a client side framework. Both frameworks are actually doing completely different things.

However, if you want to combine the use of Laravel and AngularJS then you most likely wouldn't be using all of Laravel's features. The recommended strategy is to implement something akin to a REST API using Laravel's Resource routes - and to treat all interactions as AJAX API calls.

This means you won't really be using the V of Laravel; I.e Views with Blade. (Apart from perhaps the layout for initially loading the app)

A quick google of "Laravel and AngularJS" returns a whole plethora of articles, but perhaps the best resource is this sample project:

Github example: https://github.com/bktz/Laravel-4-AngularJS-Starter-Site

A noteworthy article was this one though:

http://scotch.io/tutorials/php/create-a-laravel-and-angular-single-page-comment-application

It discusses the proposed structure of such an app, as well as the resource routes I mentioned above.

1

The best practices say while using both MVC and angularJS, we should use MVC web api at server side to get data from server And at client side we should use Angularjs framework for binding JSON data with html controls.

On the client: On the client, I used AngularJS to handle everything from calling the web api's to local validation. It gives the user the "immediate" response that is being pushed by the latest push on the web. You can make your page dynamic or relatively static. By using an architecture that is using a separation of concerns, I can replace the back end with anything that supports a RESTful api.

On the Server : I found the easiest thing to do was to build a bunch of RESTful web api's to provide data services to the client. I also perform validation on the server to make sure that no one cheats (sending data using a client other than the web page). I also assemble the web page (though a controller and minimal view) to load all of the libraries it needs. Since I am building the web page with almost no changes from the view to the final HTML, I should be able to replace AngularJS with something else.

1

If you want to use AngularJS on the client side and Laravel on the backend is to first build a start page where your application loads and lives. And around that to build JSON-services (in laravel or some other, controller methods that handles responses and requests, your CRUD).

If you have a lot of different objects (tables?), make a start view for each one of them.

It's all different layers. The client-side MVC js-framework is optional, sure it can help a lot and structure an application, but you can have application structure without frameworks also. But it's up to you. If you feel compelled to those technologies, go ahead and start coding!

0

Disclaimer: I am not familier with PHP.

  • For ASP.NET, it is a common practice to use ASP.NET MVC (+ Web API) and AngularJS together.

The routing should not be an issue, as

  • all the routing requests will first be received by AngularJS.
  • Only the service calls from Angular JS would reach Server side MVC.

Typically, Serverside MVC will handle Authentication / Authorization for all routes, and everything else would be handled by AngularJS application.

0

MVC/MVVM Frameworks on the client-side are totally separated from the frameworks that you're using on the server-side. Without a back-end for AngualJS for example you won't get that much functionality for your application. The back-end could be NodeJS app or Laravel or Zend Framework or anything else.

Your approach is totally valid to make use of an MVC frameowork on the server-side and an MVC/MVVM on the client-side, however there isn't any general best practices to mention here as your question is to broad in that sense.

  • That's not true that you need Angular JS for making rich functionality in the browser, that is incorrect. – marko Oct 9 '14 at 18:38
  • @marko Could you please elaborate a little bit? Also I'll be happy to see where I've used Need in my answer. :) – Mahdi Oct 9 '14 at 19:18
  • You can do everything in plain javascript. – marko Oct 10 '14 at 6:37
  • 1
    @marko Well, if you read the question again, you might notice this in the very last line: Are there some good approaches/practices if one wants to combine these frameworks together?. So I'm not really sure how does your comment relate to this questions ... Did you somehow got the feeling that here we are talking about naked-javascript vs. frameworks or what? :) – Mahdi Oct 10 '14 at 12:44
  • Yes, using AngularJS in the front and something else in the back is totally valid. But I think this answer misses the central problem: Doesn't using two MVC frameworks mean I have to implement each page twice? What can I do to avoid this bloat? – amon Feb 23 '15 at 17:16
-3

You can use angularjs for the front-end and codeigniter or another php framework for the back-end without any issues. You just have to keep in mind that both are MVC (Model-View-Controller).

Why use similar concepts but with different structures? Because angularjs works with template HTML and all views are HTML (not including the header and footer). As an example, with a form contact you just have to specify on the template HTML the div for ng-view (to load the views angularjs). And on the nav for the application you have to specify the controller and source view and on the app.js have you code for provider the router wich template have to load each time.

For codeigneter you have to create the controller and model to create the interaction with the database if you need it. But keep in mind that codeigneter can define routes more easily. You just have to load the views with an extension of .php where that views will have only the HTML like angular views.

You can work with multiple frameworks like CSS + angularjs + codeigneter or other MVC framework.
For forms validation, you can use the forms validation from the framework CSS and not use the angularjs. And just use angularjs to communicate with the framework MVC and to send the data using JSON.

I don't know if you follow my idea but in technology you can integrate everything to create a robust and strong app. But sometimes you will take only a part of each framework and not use all 100% of it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.