我目前使用PHP与Backbone.js和在某些地方,我使用PHP与我的MySQL服务器交互,并填充前端与Backbone/javascript。
这是我要执行的示例PHP。
$query="select id,username,message,useremail,date from contact";
$result =mysqli_query($connection,$query);
if ($result) {
echo '<table id="hor-minimalist-b" summary="Users List">
<thead>
<tr>
<th scope="col">User ID</th>
<th scope="col">User Name</th>
<th scope="col">User Message</th>
<th scope="col">User Email</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody>';
while($resultarray=mysqli_fetch_array($result)){
echo "
<tr>
<td>{$resultarray['0']}</td>
<td>{$resultarray['1']}</td>
<td>{$resultarray['2']}</td>
<td>{$resultarray['3']}</td>
<td>{$resultarray['4']}</td>
</tr>
";
}
echo "</tbody>
</table>";
}
一种方法是创建一个PHP文件并发出ajax请求以获得HTML输出并附加在div
容器中。但是由于我有太多的PHP代码片段,我想以某种方式使用javascript来缓解我,因为我正在实现backbone (http://myurl/#link/feed)。
目前,我尝试了一种丑陋的方式:使用HTML和通过PHP的echo
调用javascript函数
我的评论总结:
逐步整合骨干到现有的PHP网站是搬起石头砸自己的脚。当使用正确时,主干会发光在一个单页应用程序中,从RESTful API提供数据。
你需要的是用PHP做一个REST API,公开你的数据从服务器通过不同的url端点使用骨干模型和集合。PHP可以返回任何东西,并不局限于HTML你可以返回javascript可以轻松解析的JSON。
如何在PHP中使用Backbone
后端API
这是一个基于你的代码的非常简单的PHP端点。
example_ajax.php
<?php
$query ="select id,username,message,useremail,date from contact";
$result = mysqli_query($query);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
// return the array as JSON so Backbone can automatically parse it.
print json_encode($rows);
参见在PHP中创建一个简单的REST API
有一些PHP框架可以帮你处理REST API,比如:- Silex(基于Symfony的微框架)
- 苗条
- 流明(轻量级Laravel)
- Phalcon
前端数据处理
创建一个自定义集合,绑定到我们新创建的端点:
var ContactCollection = Backbone.Collection.extend({
url: "example_ajax.php",
});
静态模板并使用Backbone视图处理HTML模板。
首先准备HTML模板,可以是服务器上的静态HTML。
<table id="hor-minimalist-b" summary="Users List">
<thead>
<tr>
<th scope="col">User ID</th>
<th scope="col">User Name</th>
<th scope="col">User Message</th>
<th scope="col">User Email</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script type="text/template" id="contact-template">
<td><%= id %></td>
<td><%= username %></td>
<td><%= message %></td>
<td><%= useremail %></td>
<td><%= date %></td>
</script>
<<h2>骨干意见/h2>然后视图:
var ContactView = Backbone.View.extend({
tagName: 'tr',
template: _.template($('#contact-template').html()),
initialize: function(options){
// optional, automatically remove the html of this row only.
this.listenTo(this.model, 'remove', this.remove);
},
render: function() {
this.$el.empty().append(this.template(this.model.toJSON()));
return this;
},
});
下面的列表视图使用ContactView
作为子视图。
var ContactListView = Backbone.View.extend({
el: "#hor-minimalist-b", // uses existing element in the page
initialize: function(options) {
// cache a jQuery element to use as a container for the child views.
this.$body = this.$('tbody');
// optional, automatically adds a new contact when the collection changes
this.listenTo(this.collection, 'add', this.renderContact);
},
render: function() {
this.$body.empty();
this.collection.each(this.renderContact, this);
return this; // always return this for chaining
},
renderContact: function(model) {
var view = new ContactView({ model: model });
this.$body.append(view.render().el);
},
});
如何使用
var collection = new ContactCollection(),
view = new ContactListView({ collection: collection });
view.render();
collection.fetch();
.fetch()
函数对http://www.example.com/example_ajax.php
之类的东西进行GET调用,它应该返回一个数组。
为什么是API?为什么不从JavaScript发送SQL查询?
JavaScript运行在客户端,你应该永远不要信任。相反,您可以在服务器上公开您可以信任的特定端点。这就是为什么你需要一个API。
从javascript发送SQL查询是一个坏主意,原因如下:
- SQL注入:有人可以获取/更改数据库中的任何内容(包括窃取密码,或将数据库全部销毁),
- 这是一扇通往你的服务器的大门,
- 缺乏可信验证,或者更难验证SQL查询字符串,
- 紧密耦合,例如使不同客户端(移动应用程序,网站,桌面应用程序等)之间更难共享查询代码
可以这样做,但不应该这样做。
phpMyAdmin是一个应用程序的例子,它接受用户编写的SQL并按原样运行。
如果它在一个受控的环境中,比如本地内部网,并且你想从客户端JavaScript访问MySQL,你可以编写一个php脚本,它接受请求的主体并将其直接传递给MySQL数据库,以JSON形式返回结果。
例如,有一个名为Squel.js的库用于构建SQL查询字符串。他们在首页有一个大红框,上面写着:
注意:建议不要在浏览器端创建查询在服务器上运行,因为这大大增加了暴露于SQL注入攻击的风险。
Additionnal阅读:
- 有什么理由不直接从客户端Javascript到数据库?
- JavaScript可以连接MySQL吗? 什么是RESTful编程?设计实用的RESTful API的最佳实践
简短的回答是:你不能。
PHP在服务器端执行,JavaScript在客户端执行。一旦页面到达浏览器,所有的PHP都被解释了。您最好的选择是将AJAX请求发送到返回JSON的服务,然后使用JavaScript处理和显示。如果我理解正确,您希望在某些地方使用PHP加载您的网页的某些部分。这是可以做到的,但根据我的经验,这很复杂,而且不理想。这是一个非代码的解释,你可以做什么。
- 使用PHP渲染你的HTML/JavaScript页面。(example.php) 有一个JavaScript函数使用Ajax调用PHP页面。您调用的PHP页面应该只是回显您想要动态创建的所有HTML。您可以创建一个大的字符串变量,并在准备好返回时对其进行回显。该回显将返回给JavaScript的Ajax调用。(example_ajax.php生成你想要的HTML并返回)
- 根据需要使用从example_ajax.php收到的响应JavaScript来更新页面。