php做为后台程序只提供数据,全部的展现都交给前台
而前台中数据和界面dom的双向绑定使用angular。javascript
前端全给angluar进行掌控,php做为后端只须要给angular提供数据
通常来讲,jquery是用$.get来和后台交互,angular是用$http , ngResource(RESTful APIs)来进行后台交互
那么我不想这样,我但愿前端直接调用后台函数。
我想到了 hprose
因而前台我这样设计php
<html> <head> <script type="text/javascript" src="hprose.js" flashpath="/"></script> <script src="//cdn.bootcss.com/angular.js/1.4.4/angular.js"></script> <script> var server = new HproseHttpClient("http://localhost/index.php", ["mainInit"]); angular.module("demo" , []) .controller( 'demoController' , function($scope){ $scope.contents = "hehe"; server.mainInit("User@login", {name:"personOne" , password:"1478520"} , function(result) { $scope.contents = result; $scope.$apply(); }); }); </script> </head> <body ng-app="demo"> <div ng-controller="demoController"> <h1 >{{contents}}</h1> </div> </body> </html>
里面的server是一个全局的变量,以保证我能在angluar的各各controller中进行调用
server的mainInit方法(也是后台php的入口函数)
第一个参数是调用的类和方法名
第二个参数是方法须要的参数
第三个参数是成功的回调函数css
(具体的hprose的 js和php的用法你们请看官网的github,特别里面的README )html
下面要说后台了,前端
//index.php <?php require_once('../src/Hprose.php'); require_once( __DIR__ .'/vendor/autoload.php'); function mainInit($class_action,$args) { $info = explode('@' , $class_action); $className = "app\\".$info[0]; $method = $info[1]; $object = new $className; return $object->$method($args); } $server = new HproseHttpServer(); $server->setErrorTypes(E_ALL); $server->setDebugEnabled(); $server->addFunction('mainInit'); $server->start();
这个的写法就是 hprose 的server端写法
这里我用了composer的自动加载 ( composer请看岁寒老师的博客)
个人想法就是js直接调用php的函数,进行获取数据至于怎么调用php,这个交给hprose.
后台要作的就是获取数据交给前台
至于后台如何进行规划,就相似与通常的php框架了
我以为这个想法有点相似与restful,但也不太同样,
好比我不须要考虑请求方式对应的操做方式.
之因此有这个想法是最近公司要用meteor,meteor给了我这个启发。
好比若是前端的数据变更了
使用angular的 watchjava
$scope.$watch('user' , function($new_data, $old_data){ server.mainInit("User@update" , $new_data ,function(){{ }}); })
个人本意是使用php模拟meteor作一个相似的东西,
今天的这个思路是一个副产品,不过我以为这个方式进行项目开发的话也是能够
你们有什么建议吗?jquery