管理后台-后端-PHP篇

article/2025/10/23 4:07:04

这里选用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


我们先搭建基础环境

  • 软件环境:
    PHP5.6
    Codeigniter3.0.3
    codeigniter-restserver

安装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
<?phpdefined('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
<?phpclass 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

首先我们从https://github.com/chriskacerguis/codeigniter-restserver下载并解压源码包(假设已经解压到了/tmp/),复制相关文件到对应目录

> 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
<?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
<?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<$page_count,"previous" => $page>1,"results" => $results,];$this->response($data);}}
}

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

<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
$.get("http://172.17.9.177:8000/news/");
</script>

在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搭建管理后台


http://chatgpt.dhexx.cn/article/vhw9xqyY.shtml

相关文章

PHP后台管理系统

项目介绍 一款 PHP 语言基于 ThinkPhp6.x Layui MySQL等框架精心打造的一款模块化、插件化、高性能的前后端分离架构敏捷开发框架&#xff0c;可用于快速搭建前后端分离后台管理系统&#xff0c;本着简化开发、提升开发效率的初衷&#xff0c;框架自研了一套个性化的组件&am…

企业级PHP后台开发框架

项目介绍 一款 PHP 语言基于 ThinkPhp6.x Layui MySQL等框架精心打造的一款模块化、插件化、高性能的前后端分离架构敏捷开发框架&#xff0c;可用于快速搭建前后端分离后台管理系统&#xff0c;本着简化开发、提升开发效率的初衷&#xff0c;框架自研了一套个性化的组件&am…

【web前端开发】后台PHP

文章目录 PHP基本知识PHP基本语法1.php变量、常量2.php打印输出3.php运算符 if、switch语句array数组数组声明数组键名定义的规则数组的赋值操作php常见数组的分类1.数值数组2.关联数组3.多维数组 循环结构与数组遍历数组遍历--forbreak && continueforeach语句1.遍历一…

PHP从前端到后台整站开发实战笔记-1

前言 从慕课网上搞了一套ThinkPHP整站开发从前端到后台的视频教程,好好系统的学习一下MVC模式下的正规开发流程,做一下学习过程中的开发笔记. 1.系统概述 这套教程视频中使用了ThinkPHP框架,但是有点老了,用的还是3.2.3版本的,不过思路很重要,等全做好了可以考虑再用最新的5…

PHP之后台开发需要做什么——即时通信IM

关于“即时通信IM”&#xff0c;腾讯云的官方文档&#xff0c;是这样介绍的&#xff1a;即时通信&#xff08;Instant Messaging&#xff0c;IM&#xff09;基于QQ 底层 IM 能力开发&#xff0c;仅需植入 SDK 即可轻松集成聊天、会话、群组、资料管理能力&#xff0c;帮助您实现…

通过数据泵expdp、impdp方式备份与还原(导出与导入)Oracle数据库

作者专注于Java、架构、Linux、小程序、爬虫、自动化等技术。 工作期间含泪整理出一些资料&#xff0c;微信搜索【程序员高手之路】&#xff0c;回复 【java】【黑客】【爬虫】【小程序】【面试】等关键字免费获取资料。 目录 前言 一、备份 step1&#xff1a;使用system登…

expdp的用法

exp/imp与expdp/impdp的用法区别 1&#xff1a;把用户usera的对象导到用户userb,用法区别在于fromuserusera touseruserb ,remap_schemausera:usera 。例如 imp system/passwd fromuserusera touseruserb file/oracle/exp.dmp log/oracle/exp.log; impdp system/passwd directo…

expdp/impdp 详细参数解释

数据泵导出实用程序提供了一种用于在 Oracle 数据库之间传输 数据对象的机制。该实用程序可以使用以下命令进行调用: 示例: expdp scott/tiger DIRECTORYdmpdir DUMPFILEscott.dmp 您可以控制导出的运行方式。具体方法是: 在 expdp 命令后输入 各种参数。要指定各参数, 请使用关…

Oracle详解:exp/imp和expdp/impdp的区别

Oracle详解:exp/imp和expdp/impdp的区别 结论&#xff1a; 在10g之前&#xff0c;传统的导出和导入分别使用EXP工具和IMP工具&#xff0c;从10g开始&#xff0c;不仅保留了原有的EXP和IMP工具&#xff0c;还提供了数据泵导出导入工具EXPDP和IMPDP。 1、exp和expdp最明显的区…

使用expdp(非本地)远程导出数据

背景&#xff1a; 前段时间&#xff0c;需要从异地一个测试数据库中将测试数据(一张表)导入本地库&#xff0c;表数据量大约500万&#xff0c;字段160多个&#xff0c;开始用了exp/imp方式&#xff0c;速度奇慢&#xff0c;不能忍&#xff0c;于是转而使用expdp/impdp方式。 e…

运维 —— expdp、impdp

一、exp/imp与expdp/impdp 1&#xff09;exp和imp是客户端工具程序,它们既可以在可以客户端使用,也可以在服务端使用。 2&#xff09;expdp和impdp是服务端的工具程序,他们只能在ORACLE服务端使用, 不能在客户端使用。 3&#xff09;字符集的问题到expdp/impdp数据泵已经消除…

expdp / impdp 用法详解

一、注意事项&#xff1a; EXP和IMP是客户端工具程序&#xff0c;它们既可以在客户端使用&#xff0c;也可以在服务端使用。 EXPDP和IMPDP是服务端的工具程序&#xff0c;他们只能在ORACLE服务端使用&#xff0c;不能在客户端使用。 IMP只适用于EXP导出的文件&#xff0c;不适…

expdp/impdp 数据泵导入导出

业务场景&#xff1a; 在工作中&#xff0c;涉及到oracle数据库迁移&#xff0c;使用navicat等其他工具 容易报错&#xff0c;一系列问题很麻烦&#xff0c;今天记录一下操作流程及个人理解&#xff08;文章参考了很多文档和博客&#xff09;&#xff1b; EXPDP数据导出 请自…

rest_framework--过滤器filters--搜索

rest_framework–过滤器filters–搜索 view from rest_framework.pagination import PageNumberPagination from rest_framework import mixins from rest_framework import viewsets from rest_framework import filtersfrom .models import Goods from .serializers import G…

gateway整合filters

Unable to find GatewayFilterFactory with name AddRequestParamter 首先确定你要做的是用nacos整合gateway&#xff0c;然后使用gateway配置filters对请求进行修改。 一、首先在gateway微服务的application.yml或者bootstrap.yml文件进行filter的配置 // A code block ser…

php apply filters,WordPress学习——apply_filters()详解

前两天学习了 add_filters() 函数,详见>>> WordPress学习——add_filter()详解 ,今天趁热打铁再来学习下它的使用。一般情况下 add_filters() 有两种方式触发,一种是当程序运行到特定时候 WordPress 程序会判断用户是否对某些参数做了新的定义,如果有则优先使用用…

filters过滤器

这是通过过滤器写的一个只显示10个字的效果图。 1&#xff09;&#xff1a;什么时候用到过滤器&#xff1f; 绑定一些复杂数据类型的时候用到过滤器 2&#xff09;&#xff1a;以下两种方式定义过滤器&#xff1a; 第一种&#xff1a;Vue.filter()定义全局过滤器&#xff0…

vue filters的使用

filters 过滤器&#xff1a;「Vue.js 允许你自定义过滤器&#xff0c;可被用于一些常见的文本格式化。过滤器可以用在两个地方&#xff1a;双花括号插值和 v-bind 表达式 (后者从 2.1.0 开始支持)。过滤器应该被添加在 JavaScript 表达式的尾部&#xff0c;由“管道”符号指示&…

【前端】Vue---过滤器filters详解

一、认识过滤器: 格式化数据的一个工具 过滤器的作用:用于文本格式化,仅仅作为格式化操作,不做复杂的业务处理过滤器的使用方式:通过管道符号(|)来使用过滤器的使用位置:mustache插值和v-bing的表达式 二、过滤器的使用 //如何使用使用过滤器&#xff1f;我们用一个叫做管道…

Fiddler工具 — Fiddler过滤器(Filters)详解

1、Filters介绍 Filters&#xff1a;过滤器&#xff0c;帮助我们过滤请求。 如果需要过滤掉与测试项目无关的抓包请求&#xff0c;更加精准的展现抓到的请求&#xff0c;而不是杂乱的一堆&#xff0c;那功能强大的 Filters 过滤器能帮到你。 总结&#xff1a;Filters过滤器的…