这里选用codeigniter做为web框架,因为它安装方便(独立压缩包解压即可)、自身集成了migration,query-builder(对数据库的封装,不用关心数据库选型),虽然CI官方说明PHP版本只要求5.2,但是CI的第三方库ci-restserver要求5.4以上,另外PHP5.4以后加入了一个功能Built-in web server,我们可以脱离apache,直接在命令行通过php -S指令启动PHP测试服务器,本文的PHP版本将使用5.6

我们先搭建基础环境

安装PHP

> rpm -Uvh http://repo.webtatic.com/yum/el6/latest.rpm

> yum install -y php56w php56w-pdo

纠正PHP的时区

# /etc/php.ini

...

date.timezone = 'Asia/Shanghai'

安装CodeIgniter

> wget https://github.com/bcit-ci/CodeIgniter/archive/3.0.3.zip

> unzip 3.0.3.zip

> cd CodeIgniter-3.0.3/

> php -S 0.0.0.0:8000

访问该后台,顺利的话应该出现如下界面

数据库建模

1)配置数据库连接,并配置自动加载数据库模块

// application/config/database.php

$db['default'] = array(

...

'hostname' => 'sqlite:'.APPPATH.'db.sqlite3',

'dbdriver' => 'pdo',

...

);

// application/config/autoload.php

...

$autoload['libraries'] = array("database");

...

2)启动migration,默认CI使用时间戳规定版本号,简单起见我们使用序列号sequential模式

// application/config/migration.php

$config['migration_enabled'] = TRUE;

$config['migration_type'] = 'sequential';

3)编写News表的migration

// application/migrations/001_add_news.php

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Add_News extends CI_Migration {

public function up() {

$this->dbforge->add_field(array(

'id' => array(

'type' => 'INT',

'constraint' => 5,

'unsigned' => TRUE,

'auto_increment' => TRUE

),

'title' => array(

'type' => 'VARCHAR',

'constraint' => '100',

),

'content' => array(

'type' => 'TEXT',

'null' => TRUE,

),

'create_time' => array(

'type' => 'DATETIME',

),

));

$this->dbforge->add_key('id', TRUE);

$this->dbforge->create_table('news');

}

public function down() {

$this->dbforge->drop_table('news');

}

}

4)创建migrate入口

// application/controllers/Migrate.php

class Migrate extends CI_Controller {

public function index() {

$this->load->library('migration');

if (!$this->migration->latest())

{

show_error($this->migration->error_string());

}

}

}

5)CLI执行migrate

> php index.php migrate

用codeigniter-restserver搭建最简单的CRUD

> cp /tmp/codeigniter-restserver-master/application/libraries/Format.php application/libraries/

> cp /tmp/codeigniter-restserver-master/application/libraries/REST_Controller.php application/libraries/

> cp /tmp/codeigniter-restserver-master/application/config/rest.php application/config/

> cp /tmp/codeigniter-restserver-master/application/language/english/rest_controller_lang.php application/language/english/

编写CRUD的controller

// application/controllers/News.php

require APPPATH . '/libraries/REST_Controller.php';

class News extends REST_Controller{

public function index_post(){

$this->db->set("title",$this->post("title"));

$this->db->set("content",$this->post("content"));

$this->db->set("create_time",date('Y-m-d H:i:s'));

$this->db->insert("news");

$this->set_response(['id'=>$this->db->insert_id()], REST_Controller::HTTP_CREATED);

}

public function index_get(){

$id = $this->get("id");

if($id!=null){

$this->response($this->db->get_where("news",array('id'=>$id))->result()[0]);

}else{

$this->response($this->db->get("news")->result());

}

}

public function index_put(){

$this->db->set("title",$this->put("title"));

$this->db->set("content",$this->put("content"));

$this->db->where("id",$this->get("id"));

$this->db->update("news");

$this->set_response('', REST_Controller::HTTP_NO_CONTENT);

}

public function index_delete(){

$this->db->where("id",$this->get("id"));

$this->db->delete("news");

$this->set_response('', REST_Controller::HTTP_NO_CONTENT);

}

}

// application/config/routes.php

...

$route['news/(:num)'] = 'news/index/id/$1';

接下来我们需要用postman来测试接口(如何使用postman工具测试不在本文范畴),伪测试代码如下

> curl POST /news/ #Create

> curl GET /news/ #Read list

> curl GET /news/:id/ #Read item

> curl PUT /news/:id/ #Update

> curl DELETE /news/:id/ #Delete

接下来我们来整合BasicAuth验证功能

// application/config/rest.php

...

$config['rest_auth'] = 'basic';

$config['auth_source'] = '';

...

仅为演示,我们在rest.php里面配置了默认的帐号admin,密码是默认的1234。

我们再访问一下接口,服务器将返回401错误,浏览器根据该错误将弹出认证界面

按照制定的接口文档,我们再补全一个认证接口给客户端登录使用

// application/controllers/Auth.php

require APPPATH . '/libraries/REST_Controller.php';

class Auth extends REST_Controller{

public function info_get(){

$this->response([

'name' => 'admin',

]);

}

}

书写扩展的协议-批量删除

// application/controllers/News.php

...

class News extends REST_Controller{

...

public function deletes_delete(){

$ids = split(",",$this->input->get("ids"));

foreach($ids as $id){

$this->db->where("id",$id);

$this->db->delete("news");

}

$this->set_response('', REST_Controller::HTTP_NO_CONTENT);

}

}

书写扩展协议-搜索+分页

// application/controllers/News.php

...

class News extends REST_Controller{

public $PAGE_SIZE = 50;

...

public function index_get(){

$id = $this->get("id");

if($id!=null){

$this->response($this->db->get_where("news",array('id'=>$id))->result()[0]);

}

else{

$search = $this->get("search");

$page = $this->get("page");

if($page==null)$page="1";

$page = intval($page);

$this->db->like('title',$search);

$total_count = $this->db->count_all_results("news");

$page_count = floor($total_count/$this->PAGE_SIZE);

if($total_count%$this->PAGE_SIZE>0)$page_count++;

$this->db->like('title',$search);

$this->db->order_by("create_time",'DESC');

$offset = $this->PAGE_SIZE*($page-1);

$this->db->limit($this->PAGE_SIZE,$offset);

$results = $this->db->get("news")->result();

$data = [

"total_count" => $total_count,

"page_count" => $page_count,

"next" => $page

"previous" => $page>1,

"results" => $results,

];

$this->response($data);

}

}

}

后端基本完成了,还剩下最后一个问题CORS,我们在本地写一个简单的jquery

在chrome里面调试一下会出现如下错误

XMLHttpRequest cannot load http://172.17.9.177:8000/news/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401.

我们改造构造函数来加入CORS处理

// application/controllers/News.php

class News extends REST_Controller{

...

function __construct() {

header('Access-Control-Allow-Origin: *');

header("Access-Control-Allow-Headers: Authorization,Content-Type");

header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");

$method = $_SERVER['REQUEST_METHOD'];

if($method == "OPTIONS") {

die();

}

parent::__construct();

}

}

同样方法处理Auth.php,再次用jquery测试,之前那个错误将不再出现。

此文数据库模型、协议的定义参见用Angular搭建管理后台

php后端管,管理后台-后端-PHP篇相关推荐

  1. 管理后台-后端-PHP篇

    这里选用codeigniter做为web框架,因为它安装方便(独立压缩包解压即可).自身集成了migration,query-builder(对数据库的封装,不用关心数据库选型),虽然CI官方说明PH ...

  2. 3. 在WordPress管理后台撰写第一篇博客文章

    3. 发布第一篇博客文章 在前面的文章中,我们首先介绍了1. 购买云服务器和域名的基本操作,然后又成功2. 搭建最简单的博客网站,如果你还没完成上面这些操作,请提前点击查看. 这篇文章我们首先熟悉下网 ...

  3. Qt Creator管理C ++后端对象

    Qt Creator管理C ++后端对象 管理C ++后端对象 管理C ++后端对象 许多应用程序提供了用C ++实现的QObject对象,这些对象充当QML和C ++之间的桥梁.此类对象通常会向QM ...

  4. 如何用AngularJS构建管理后台

    前言 一般来说,一个管理后台的构建的基本流程是:(1)选择一个开发语言->(2)选择一种数据库->(3)选择一个开发轮子(framework)->(4)在轮子上搭建前端框架-> ...

  5. 基于VUE+DJANGO开发的前后端分离的官方网站系统带管理后台

    前言 每个企业都有开发一个官方网站的需求,用于展示企业的产品和服务,企业的文化宗旨和品牌形象等,并要求网站有比较强的自由定制的功能.为此,我开发了这款官方网站系统,自带轻便的管理后台,在后台简单修改下 ...

  6. 一文搞懂前台,后台,中台,前端,后端,管理端,业务端,技术中台,业务中台,数据中台,物联网中台到底是什么?

    1. 前台/前端 前台 (Frontend):是指用户直接面对的系统界面部分,包括用户界面设计.页面交互逻辑.数据呈现和用户操作等,主要职责是与用户打交道,用友好的交互方式把闭门造车的后台功能暴露出来 ...

  7. 基于shiro+jwt的真正rest url权限管理,前后端分离

    代码地址如下: http://www.demodashi.com/demo/13277.html bootshiro & usthe bootshiro是基于springboot+shiro+ ...

  8. 视频教程-springboot+Vue整合前后端分离权限后台管理系统-Java

    springboot+Vue整合前后端分离权限后台管理系统 拥有八年的Java项目开发经验,擅长Java.vue.SpringBoot.springCloud.spring.springmvc.myb ...

  9. SpringBoot2.x炫酷吊炸天前后端分离的后台管理系统

    点击▲关注 "爪哇笔记"   给公众号标星置顶 更多精彩 第一时间直达 项目简介 一个基于 Spring Boot 2.1.0 . Spring Boot Jpa. JWT.Spr ...

最新文章

  1. GlassFish 4带来了Java EE 7
  2. 混合App开发,HBuilder开发移动App
  3. 1982:【19CSPJ普及组】数字游戏 方法二
  4. 深度学习《CGAN模型》
  5. openGauss北京Meetup成功举办,“产学研用”合力共建主流根社区(附:视频回放PPT)...
  6. java中的过滤器:Filter
  7. 从零开始用python处理excel视频_书榜 | 计算机书籍(6.29-7.5)销售排行榜
  8. nacos需要mysql吗_nacos无法连接mysql的解决方法
  9. 通过Git WebHooks+脚本实现自动更新发布代码之Shell脚本(二)
  10. 嵌入式开发之GCC编译器使用
  11. twincat3授权
  12. 经典坦克大战——C++实现(附源码)
  13. php本地解密,PHPDecode 在线解密工具
  14. 360网上商城:链接生态与终端
  15. python用逗号隔开输出_c语言提取逗号隔开的 python输出用逗号隔开的数字
  16. 苹果高管公然“开怼”:三星抄袭 iPhone,只加了个大屏
  17. one 主格 复数 宾格_人称代词的主格和宾格
  18. 语音识别ASR - HTK(HResults)计算字错率WER、句错率SER
  19. PLSQL无客户端连接ORACLE
  20. ACM-Giroro的地雷测试(AC,广度优先搜索)

热门文章

  1. Linux+C 开发基础
  2. 数组,异质结构以及指针的详解
  3. usart hmi(串口屏)介绍
  4. C++ 打印表格到屏幕或文件
  5. 币图告诉你如何解决双花问题
  6. 记一次Redis scard读取数据结果不对的问题【DaemonCoder】
  7. 电脑启动计算机无法启动 修复工具栏,如何利用Win7启动修复功能解决电脑启动进不了系统的问题...
  8. Vue中使用Ckplayer播放器
  9. LUA脚本(游戏辅助) 点击函数 区域单点多点着色
  10. 干货!最全的AI速查表|神经网络,机器学习,深度学习