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

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

问题描述

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 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?

我不谈论使用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应用程序,也就是一个网页,更像一个应用程序,并在很大程度上依赖于客户端渲染,你应该考虑让客户维护管理状态的所有责任和presentation。这将是容易维护,并且将更加用户友好。

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输出pre-渲染的视图,并使用角度为单纯的DOM操作,你应该考虑让PHP后台输出应该在REST风格采取行动的数据,并有角present它。

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:

/用户/帐户

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 /认证/

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

和角,你可以做一个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中心的方式,因为这将是更正确的分离conserns并且会更容易维护。

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.

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

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