4

I'm a bit confused on how to develop a web application using frameworks on both front-end (e.g. Angular) and back-end (e.g. Phalcon). Phalcon's documentation has bits like:

<?php

echo "<h1>Hello!</h1>";

echo Phalcon\Tag::linkTo("signup", "Sign Up Here!");

But wouldn't I be using Angular's {{ }} templating notation to print things? So if I'm using Angular, do I really need to use Phalcon, or Laravel, or any other PHP MVC?

I guess I'm trying to ask on a very basic level: if I'm using Angular, can't I just use single PHP files on the back-end and not use a PHP MVC?

(sorry, I'm not sure how to word this question; I may re-edit)

CaptSaltyJack
  • 15,283
  • 17
  • 70
  • 99

3 Answers3

6

The short answer to your question is: yes, you can just use a simple, non-mvc backend for your angular applications. In fact, that is a good thing to do.

You can still use an MVC framework if you like, but dynamically rendering data into HTML on the server side is something to avoid (though there are some exceptions).

I tend to recommend using a server-side language for exposing RESTful APIs which just respond with JSON data and then using static html/css/js (Angular) to handle user interactions, navigation, view states, data retrieval, etc.

bendalton
  • 778
  • 6
  • 8
2

You can use MVC both in the frontend (AngularJS) and in the backend. As @bentaldon wrote you can have an API that will offer data back to AngularJS.

The API (backend) application can easily have a very streamlined MVC framework with the View component rendering data in a format that you need in your AngularJS application (say JSON)

Should you decide to use a backend not in the form of an API then you can easily change the {{}} templating notation of AngularJS to avoid collisions.

An example that I can give you can be found here:

https://github.com/niden/phalcon-angular-harryhogfootball

Using AngularJS and Phalcon at the same time.

Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67
  • Missing URL in your post? – CaptSaltyJack Oct 10 '13 at 14:53
  • 1
    Odd. I added it in the first place but it didn't appear. Corrected now. Thanks for the pointer – Nikolaos Dimopoulos Oct 10 '13 at 15:33
  • 1
    @nikolaos your touched on a concept partially missed by bendaltons answer (who went onto only a half mvc answer, where the user asked for mvc/mvc) , and got no credit for it, i wantedt upvote you. Instead of saying yes, you made it very clear, e.g. yes you can use a ...... more concise. But since your not getting any votes, it may be because you might want to correct your usage of the word 'back' there, maybe change it to 'forward' or 'to' ? cause i think your referring to the view, and the reader is likely to interpret the improper use of the english word "back" into actual "back end". – blamb Oct 20 '15 at 00:16
1

The Full answer:

You can have both a MVC front end, and a MVC back end. You can also have an MVC front, and no frame-worked back, AND vice-versa. You dont always need a framework, but its usually a good idea. The trick is to find the framework your looking for.

If you want to have a cool ALL JS (no PHP) app, you can use something like Deployd as your back end api / mongodb, and integrate angular in the style of MVC. If you want to MVC'itize the back-end framework of this type of JS app you can do something like this https://hackhands.com/mongodb-crud-mvc-way-with-passport-authentication/ (look down to the Express.js app/views part).

I would recommend Express.js as your back end framework, and implement MVC style using a tut like this http://briantford.com/blog/angular-express. Then the all the scripting ties into the MVC folders for the front end, which uses this express.js server. Your views to a views folder, controller(routes) to a controller folder, and all your scripts that make up the CRUD or back end API, which communicate with your mongodb to pull data in a model folder. This is very easily scaled up. If you needed Angular in this setup, you would want to follow tutorials on how to implement angular into express .

If you want to go PHP back end with either a templated php front end, or an angular MVC front end you can use something like the Symfony2 framework, which is ALMIGHTY, and super powerful very well documented, and a no-brainer. Then when you make your templates(views) using Twig(symfonys default template engine), you can either code your html there, and call your php variables on that page, or you can change it up like i mentioned using angular then just call and call it just like this on your view

{% verbatim %}
    { { variabileAngularjs } }
{% endverbatim %}   

And, if what you originally asked, using single PHP files on the backend, that sounds like what you call "Procedural" coding, you can do it Object oriented too, but not sure the way you were thinking, if you go Object oriented, then its very easy just to put the models in a model folder, and a controller in a controller folder, and then its called mvc, but if you put all your functionality inside teh same PHP Method, like validating a post, goign to the db, parsing data from db, and then sending that inside an http response forward to the view sounds like a bad use of web development technologies, because it caused serious dependency issues, because you wouldn't be observing "decoupling" very well if you did this. To decouple it, you remove the method that does the db call, and return it, then you have another method, that might as well be on another file, in a controller folder, which calls this method, and returns is via http, e.g. an http response, your view can call this controller action, provoking the response. This is the way MVC wants to do it.

i know this is an older question, but it gave me the answer i needed, but i felt obligated to do a good writeup on this, since it didn't have much info on it to help support this question.

blamb
  • 4,220
  • 4
  • 32
  • 50