简体   繁体   English

使用MVC框架进行前端和后端

[英]Using MVC frameworks for front-end and back-end

I'm a bit confused on how to develop a web application using frameworks on both front-end (eg Angular) and back-end (eg Phalcon). 我对如何使用前端(例如Angular)和后端(例如Phalcon)上的框架开发Web应用程序感到困惑。 Phalcon's documentation has bits like: Phalcon的文档有点像:

<?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? 但是我不会使用Angular的{{ }}模板符号来打印东西吗? So if I'm using Angular, do I really need to use Phalcon, or Laravel, or any other PHP MVC? 所以如果我使用Angular,我真的需要使用Phalcon,Laravel或任何其他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? 我想我试图在一个非常基本的层面上提问:如果我使用Angular,我不能只在后端使用单个PHP文件而不使用PHP MVC吗?

(sorry, I'm not sure how to word this question; I may re-edit) (抱歉,我不确定如何说出这个问题;我可能会重新编辑)

The short answer to your question is: yes, you can just use a simple, non-mvc backend for your angular applications. 对您的问题的简短回答是:是的,您可以使用简单的非mvc后端来处理角度应用程序。 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). 如果您愿意,您仍然可以使用MVC框架,但是在服务器端动态地将数据呈现为HTML是可以避免的(尽管有一些例外)。

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. 我倾向于建议使用服务器端语言来暴露RESTful API,它只响应JSON数据,然后使用静态html / css / js(Angular)来处理用户交互,导航,视图状态,数据检索等。

You can use MVC both in the frontend (AngularJS) and in the backend. 您可以在前端(AngularJS)和后端使用MVC。 As @bentaldon wrote you can have an API that will offer data back to AngularJS. 正如@bentaldon所写,你可以拥有一个将数据提供给AngularJS的API。

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) API(后端)应用程序可以轻松拥有一个非常简化的MVC框架,其中View组件以AngularJS应用程序中所需的格式呈现数据(比如说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. 如果您决定使用不是API形式的后端,那么您可以轻松更改AngularJS的{{}}模板符号以避免冲突。

An example that I can give you can be found here: 我可以在这里找到一个我可以给你的例子:

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

Using AngularJS and Phalcon at the same time. 同时使用AngularJS和Phalcon。

The Full answer: 完整答案:

You can have both a MVC front end, and a MVC back end. 您可以同时拥有MVC前端和MVC后端。 You can also have an MVC front, and no frame-worked back, AND vice-versa. 你也可以有一个MVC前端,没有框架后退,反之亦然。 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. 如果你想要一个很酷的ALL JS(没有PHP)应用程序,你可以使用像Deployd这样的东西作为你的后端api / mongodb,并以MVC的风格集成angular。 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). 如果你想MVC'itize这种类型的JS应用程序的后端框架,你可以做这样的事情https://hackhands.com/mongodb-crud-mvc-way-with-passport-authentication/ (向下看Express.js app/views部分)。

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 . 我建议将Express.js作为你的后端框架,并使用像http://briantford.com/blog/angular-express这样的tut实现MVC风格。 Then the all the scripting ties into the MVC folders for the front end, which uses this express.js server. 然后,所有脚本都绑定到前端的MVC文件夹,该文件夹使用此express.js服务器。 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. 您对views文件夹,控制器(路由)到controller文件夹的views ,以及组成CRUD或后端API的所有脚本,它们与您的mongodb通信以提取model文件夹中的数据。 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 . 如果您在此设置中需要Angular,则需要遵循有关如何将角度转换为快速的教程。

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. 如果你想使用模板化的php前端或角度MVC前端进行PHP后端,你可以使用类似Symfony2框架的东西,它是ALMIGHTY,并且非常强大,非常有用,并且很简单。 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 然后当你使用Twig(symfonys默认模板引擎)制作你的模板(视图)时,你可以在那里编码你的html,并在那个页面上调用你的php变量,或者你可以改变它,就像我提到的使用angular然后只是调用和在你的观点上就像这样称呼它

{% 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. 而且,如果您最初询问的是,在后端使用单个PHP文件,这听起来像您所谓的“程序”编码,您也可以执行面向对象,但不确定您的思维方式,如果您去面向对象,然后很容易将模型放在模型文件夹中,控制器放在控制器文件夹中,然后调用它的mvc,但是如果你把所有的功能放在同一个PHP方法中,比如验证一个帖子,那么就转到数据库,从数据库中解析数据,然后将http响应中的数据发送到视图中听起来像是对Web开发技术的错误使用,因为它会导致严重的依赖性问题,因为如果你做的话,你就不会很好地观察到“解耦”这个。 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, eg an http response, your view can call this controller action, provoking the response. 要解耦它,你删除执行db调用的方法,并返回它,然后你有另一个方法,也可能在另一个文件,在控制器文件夹中,调用此方法,并返回是通过http,例如一个http响应,您的视图可以调用此控制器操作,从而激发响应。 This is the way MVC wants to do it. 这是MVC想要的方式。

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. 我知道这是一个较老的问题,但它给了我需要的答案,但我觉得有必要对此做一个好的写作,因为它没有太多的信息来帮助支持这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何同时使用AngularJS作为前端框架和ASP.NET MVC作为后端框架? - how to use AngularJS as front-end framework and ASP.NET MVC as back-end framework together? 将jhipster后端和前端分成两个项目? - Separating jhipster back-end and front-end into two projects? Web应用程序前端和后端的误解 - Web Application front-end and back-end misunderstanding 后端vs前端控制的Web应用程序 - back-end vs front-end controlled web application JS前端框架(ember,Angular等)如何与后端框架(例如Django,Rails等)一起使用? - How does JS front-end framework (ember, Angular etc.) work with back-end frameworks(such as Django , Rails etc.)? 使用laravel作为后端API和AngularJS作为前端的实时通知 - Real-Time notification using laravel as back-end API and AngularJS as front-end 如何使用rest API连接AngularJS前端和Python后端? - How to connect AngularJS front-end and Python back-end using rest API? 如果我们使用前端 Angularjs 和后端 spring-boot,则被 CORS 策略处理阻止? - blocked by CORS policy handling if we using front-end Angularjs and back-end spring-boot? 带有PHP + MySQL后端的JS(Angular)前端用户特权 - JS(Angular) Front-End user privileges with PHP + MySQL Back-End Docker 上的 Angular 前端、Java 后端最佳部署模式 - Angular Front-end, Java Back-end best deployment pattern on Docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM