php好的mvc中index方法,创建一个mvc应用目录架构并创建入口文件index.php
摘要:<?php
require 'vendor/autoload.php';
require 'pig/Base.php';
define('ROOT_PATH',__DIR__.'/');
$config=require 'pig/config.php';
$queryStr=$_S
<?php
require 'vendor/autoload.php';
require 'pig/Base.php';
define('ROOT_PATH',__DIR__.'/');
$config=require 'pig/config.php';
$queryStr=$_SERVER['REQUEST_URI'];
(new \pig\Base($config,$queryStr))->run();
框架入口文件 index.php 打开执行后实例化框架基础类的调试状态,自动加载类文件如:当用户提供:php.edu/demo/index/a 后框架自动加载检测准备new的类生成绝对路径.
D:\myphp_www\PHPTutorial\WWW\php.edu/pig/Route.php
D:\myphp_www\PHPTutorial\WWW\php.edu/app/admin/controller/Index.php
/**
* * 框架基础类
* 1. 调试模式
* 2. 自动加载
* 3. 启动框架
*/
namespace pig;
class Base
{
protected $config=[];
protected $queryStr='';
public function __construct($config,$queryStr)
{
$this->config=$config;
$this->queryStr=$queryStr;
}
public function setDebug()
{
if($this->config['app']['debug']){
error_reporting(E_ALL);
ini_set('display_errors','On');
}else{
error_reporting(E_ALL);
ini_set('display_errors','off');
ini_set('log_errors','On');
}
}
public function loader($class)
{
$path = ROOT_PATH.str_replace('\\','/',$class).'.php';
echo $path.'
';
if (!file_exists($path)){
header('Location:/');
}
require $path;
}
public function run()
{
$this->setDebug();
spl_autoload_register([$this,'loader']);
echo(new Route($this->config['route']))->parse($this->queryStr)->dispatch();
}
}<?php
/**
*路由、分发类
*/
namespace pig;
class Route
{
// protected $route = [];
public $route = [];
// protected $pathInfo=[];
public $pathInfo=[];
// protected $params=[];
public $params=[];
public function __construct($route)
{
$this->route=$route;
}
public function parse($queryStr='')
{
$queryStr=trim(strtolower($queryStr),'/');
$queryArr=explode('/',$queryStr);
$queryArr=array_filter($queryArr);
switch (count($queryArr))
{
case 0:
$this->pathInfo =$this->route;
break;
case 1:
$this->pathInfo['module']=$queryArr[0];
break;
case 2:
$this->pathInfo['module']=$queryArr[0];
$this->pathInfo['controller']=$queryArr[1];
break;
case 3:
$this->pathInfo['module']=$queryArr[0];
$this->pathInfo['controller']=$queryArr[1];
$this->pathInfo['action']=$queryArr[2];
break;
default:
$this->pathInfo['module']=$queryArr[0];
$this->pathInfo['controller']=$queryArr[1];
$this->pathInfo['action']=$queryArr[2];
$arr=array_slice($queryArr,3);
for ($i=0;$i
if (isset($arr[$i+1])){
$this->params[$arr[$i]]=$arr[$i+1];
}
}
break;
}
return $this;
}
public function dispatch()
{
$module=$this->pathInfo['module'];
$controller='\app\\'.$module.'\controller\\'.$this->pathInfo['controller'];
$action = $this->pathInfo['action'];
// new $controller;
if(!method_exists($controller,$action)){
$action=$this->route['action'];
header('Location:/');
}
return call_user_func_array([new $controller,$action],$this->params);
}
}<?php
/**
* Created by PhpStorm.
* User: 普通用户
* Date: 2019/5/13
*
*/
namespace app\demo\controller;
class index
{
public function a()
{
echo "demo->index->a";
}
}
批改老师:天蓬老师批改时间:2019-05-13 09:17:42
老师总结:通过这个小框架 , 相信你对一个php框架的运行的基本流程有了一个大致的了解...
一个真实商用的php框架, 它的复杂度远高于这个教学框架, 但是底层的原理与架构, 与这个并无本质区别..
掌握 这个小框架的编程思想和思维模式, 对于你以后学习成熟的框架开发, 如laravel, thinkphp,ci, yii等, 非常有好处的, 加油...
php好的mvc中index方法,创建一个mvc应用目录架构并创建入口文件index.php相关推荐
- VS 如何创建一个MVC框架
VS 如何创建一个MVC框架 开发工具与关键技术:VS 如何创建一个MVC框架 作者:胡宁淇 撰写时间:2019年1月17日 ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务 ...
- c语言求出伍位数,简介 模拟银行的钱数大写输出例如345叁肆伍方法/步骤 首先打开VC++ 文件》》》新建 创建一个C++空白文档 先声明头文件复制代码 代码...
简介 模拟银行的钱数大写输出例如 345 叁肆伍 方法/步骤 首先打开VC++ 文件>>>新建 创建一个C++空白文档 先声明头文件 #include 声明变量 char *p[10 ...
- ThinkPHP3.2URL重写隐藏应用的入口文件index.php
可以通过URL重写隐藏应用的入口文件index.PHP,下面是相关服务器的配置参考: [ Apache ] httpd.conf配置文件中加载了mod_rewrite.so模块 AllowOverri ...
- nginx给php做统一入口,Nginx如何来配置隐藏入口文件index.php(代码)
这篇文章给大家介绍的内容是关于Nginx配置 隐藏入口文件index.php,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. Nginx配置文件里放入这段代码server { loca ...
- thinkphp5/phpstudy分析入口文件index.php及localhost和配置域名访问网站根目录www
文章目录 1.thinkphp5入口文件简介 2.访问网站根目录www (1)localhost访问网站根目录www (2)配置域名访问网站根目录www 第一步:点击站点域名管理 第二步:站点域名设置 ...
- idea的几个好用快捷键、常用配置(包括git)、jdk和javaSE和javaEE、创建一个SE工程、debug、创建一个Web工程、打war包
几个好用的快捷键(持续更新) Ctrl+Shift+f12 ,相当于myeclipse的ctrl+m 最大化窗口的 alt+shift+↑或↓,将当前行上移或下移 shift+enter ,当前行下一 ...
- index.php文件下载,此文件时入口文件index.php,_PHP教程
此文件时入口文件index.php, 此文件时入口文件index.php //定义一下ThinkPHP框架存放的路径 define('THINK_PATH','./ThinkPHP/'); //定义当 ...
- 创建一个nginx容器(提供配置文件和网页文件)
创建一个nginx容器(提供配置文件和网页文件) 创建一个nginx容器 [root@localhost ~]# docker run -tid --name nginx centos 81cd1d3 ...
- ThhinkPHP5隐藏入口文件index.php
原文链接1:https://blog.csdn.net/lw545034502/article/details/79374172/ 原文链接2:https://www.cnblogs.com/chq3 ...
最新文章
- android 父控件的背景_android控件拖动,移动、解决父布局重绘时控件回到原点
- python和java哪个-Python与Java-你首选哪个?
- 阿里云服务器,,外网访问公网IP不成功
- 七、数据库的生成和修改
- 教你控制Python多线程中线程数量
- 首个金融领域的开源中文预训练语言模型FinBERT了解下
- python label位置_Python3 tkinter基础 Label pack 设置控件在窗体中的位置
- oracle 11g ocp 笔记(15)--使用rman进行备份
- 信息学奥赛一本通 1078:求分数序列和 | OpenJudge NOI 1.5 32
- .NetCore中三种注入方式的思考
- 什么是REST?以及RESTful的实现
- 子函数实现链式队列 的 操作,创建、入队、出队、退出。
- 光盘版的DOS启动盘
- adobe bridge cs6怎么卸载_怎么取消adobe bridge cs6 更新
- python编写opencv-OpenCV官方教程中文版(for Python)pdf+自己编写的全套目录
- 每日一佳——SVM Optimization: Inverse Dependence on Training Set Size(Shai Shalev-Shwartz et al,ICML,2008)
- 小学计算机课认识键盘,教学设计:《认识键盘》
- 刘强东事件真相?牵牛队长看到一场狂欢
- RichErp生产Erp系统简介
- GIthub标星32k的面试手册长啥样?
热门文章
- UE:增强现实AR可视化基本概念
- Revit的Enscape基本培训(2021) Enscape Essential Training for Revit (2021)
- Maya阿诺德室外环境灯光照明和渲染技术学习视频教程
- 分布式一致性(共识)算法(Paxos,raft,ZAB)的一些总结
- 每日一题 -- 11-1
- [转]mysql 数据类型
- 『TensorFlow』数据读取类_data.Dataset
- webGL的一些咨询--web3D
- This is A PostXing Test
- SLAM笔记(五)光束平差法(Bundle Adjustment)