我应该将 AngularJS 与 PHP 框架混合使用吗? [英] Should I mix AngularJS with a PHP framework?

查看:20
本文介绍了我应该将 AngularJS 与 PHP 框架混合使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS 在交互式 HTML5 和模型绑定方面非常强大.另一方面,像 Yii 这样的 PHP 框架可以实现快速、结构良好、安全和强大的 Web 应用程序开发.这两种技术都为数据访问、迭代和页面布局提供了复杂的手段.

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.

混合这两种方法(客户端和服务器端页面设置")的做法是好是坏,还是违背交互式、无缝 HTML5 AJAX 网络应用程序的意义?

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?

我不是在谈论使用 PHP 生成 JS(看到这个问题)- 我说的是生成一个将使用 AngularJS 的视图.

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.

我也知道 AngularJS 页面应该(或可以)通过 REST 服务与服务器通信以获取数据(参见这个问题) 而不是直接从例如 PHP 变量中检索它.但对我来说,在 PHP 中为整个 Web 应用程序单独设计框架"似乎更方便(例如构建主菜单或处理授权/会话等)

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

推荐答案

看来您可能更喜欢用 PHP 进行开发,这让您无法充分利用 Web 应用程序的潜力.

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 渲染局部视图和整体视图,但我不推荐这样做.

It is indeed possible to have PHP render partials and whole views, but I would not recommend it.

要充分利用 HTML 和 javascript 的可能性来制作 Web 应用程序,即更像应用程序并严重依赖客户端渲染的网页,您应该考虑让客户端承担管理状态的所有责任和介绍.这将更易于维护,并且对用户更友好.

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.

我建议您以更以 API 为中心的方法进行更舒适的思考.与其让 PHP 输出预渲染的视图,并使用 angular 仅用于 DOM 操作,您应该考虑让 PHP 后端输出应该在 RESTFully 上操作的数据,并让 Angular 呈现它.

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.

使用 PHP 渲染视图:

Using PHP to render the view:

/user/account

if($loggedIn)
{
    echo "<p>Logged in as ".$user."</p>";
}
else
{
    echo "Please log in.";
}

如何使用以 API 为中心的方法通过像这样输出 JSON 来解决同样的问题:

How the same problem can be solved with an API centric approach by outputting JSON like this:

api/auth/

{
  authorized:true,
  user: {
      username: 'Joe', 
      securityToken: 'secret'
  }
}

在 Angular 中,你可以做一个 get,并处理客户端的响应.

and in Angular you could do a get, and handle the response client side.

$http.post("http://example.com/api/auth", {})
.success(function(data) {
    $scope.isLoggedIn = data.authorized;
});

为了混合客户端和服务器端,您提出的方式可能适用于维护不重要且您是单一作者的较小项目,但我更倾向于以 API 为中心的方式,因为这将更正确地分离会更容易维护.

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.

这篇关于我应该将 AngularJS 与 PHP 框架混合使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆