我应该将 AngularJS 与 PHP 框架混合使用吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15623967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I mix AngularJS with a PHP framework?
提问by Dani
AngularJS is very powerful when it comes to interactive HTML5 and model binding. On the other hand, PHP frameworks like Yii enable quick, well-structured, safe and powerful web application development. Both technologies provide sophisticated means for data access, iteration and page layouting.
AngularJS 在交互式 HTML5 和模型绑定方面非常强大。另一方面,像 Yii 这样的 PHP 框架可以实现快速、结构良好、安全和强大的 Web 应用程序开发。这两种技术都为数据访问、迭代和页面布局提供了复杂的手段。
Is it good or bad practice to mix those two approaches (client-side and server-side "page setup") or is this rather against the meaning of interactive, seamless HTML5 AJAX web applications?
混合这两种方法(客户端和服务器端“页面设置”)是好还是坏的做法,还是违背了交互式、无缝 HTML5 AJAX Web 应用程序的含义?
I am not talking about generating JS using PHP (See this question) - I'm talking about generating a view that will make use of AngularJS.
我不是在谈论使用 PHP 生成 JS(见这个问题)——我在谈论生成一个将使用 AngularJS 的视图。
I also know that an AngularJS page should (or can) communicate with the server via REST services to get data (See this question) instead of retrieving it from for example PHP variables directly. But to me it seems more convenient to design the "frame" for the entire web application separately in PHP (e.g. build the main menu or handle authorization/ sessions etc.)
我也知道 AngularJS 页面应该(或可以)通过 REST 服务与服务器通信以获取数据(请参阅此问题),而不是直接从例如 PHP 变量中检索数据。但对我来说,在 PHP 中为整个 Web 应用程序单独设计“框架”似乎更方便(例如构建主菜单或处理授权/会话等)
回答by Kenneth Lynne
It seems you may be more comfortable with developing in PHP you let this hold you back from utilizing the full potential with web applications.
似乎您可能更喜欢用 PHP 进行开发,这让您无法充分利用 Web 应用程序的潜力。
It is indeed possible to have PHP render partials and whole views, but I would not recommend it.
确实可以让 PHP 呈现部分视图和整体视图,但我不推荐它。
To fully utilize the possibilities of HTML and javascript to make a web application, that is, a web page that acts more like an application and relies heavily on client side rendering, you should consider letting the client maintain all responsibility of managing state and presentation. This will be easier to maintain, and will be more user friendly.
要充分利用 HTML 和 javascript 的可能性来制作 Web 应用程序,即行为更像应用程序并严重依赖客户端呈现的网页,您应该考虑让客户端承担管理状态和表示的所有责任。这将更易于维护,并且对用户更友好。
I would recommend you to get more comfortable thinking in a more API centric approach. Rather than having PHP output a pre-rendered view, and use angular for mere DOM manipulation, you should consider having the PHP backend output the data that should be acted upon RESTFully, and have Angular present it.
我建议您以更以 API 为中心的方法进行更舒适的思考。与其让 PHP 输出预渲染的视图,并使用 angular 仅用于 DOM 操作,您应该考虑让 PHP 后端输出应该在 RESTFully 上操作的数据,并让 Angular 呈现它。
Using PHP to render the view:
使用 PHP 渲染视图:
/user/account
/user/account
if($loggedIn)
{
echo "<p>Logged in as ".$user."</p>";
}
else
{
echo "Please log in.";
}
How the same problem can be solved with an API centric approach by outputting JSON like this:
如何使用以 API 为中心的方法通过像这样输出 JSON 来解决同样的问题:
api/auth/
api/auth/
{
authorized:true,
user: {
username: 'Joe',
securityToken: 'secret'
}
}
and in Angular you could do a get, and handle the response client side.
在 Angular 中,您可以执行 get 操作并处理响应客户端。
$http.post("http://example.com/api/auth", {})
.success(function(data) {
$scope.isLoggedIn = data.authorized;
});
To blend both client side and server side the way you proposed may be fit for smaller projects where maintainance is not important and you are the single author, but I lean more towards the API centric way as this will be more correct separation of conserns and will be easier to maintain.
将客户端和服务器端混合在一起,您提出的方式可能适用于维护不重要且您是单一作者的较小项目,但我更倾向于以 API 为中心的方式,因为这将更正确地分离问题并且将更容易维护。