博客
关于我
怎么样写一个自己的渣渣PHP框架(3)
阅读量:216 次
发布时间:2019-02-28

本文共 3232 字,大约阅读时间需要 10 分钟。

上一篇的时候大概说了自动加载。开始讲路由和配置,路由是自己写的,写的比较渣渣。不要笑哦!

路由类在Composer中也有很多,可以直接拿过来用。配置也很简单。自己百度谷歌一下去吧!这里就不说了。为了方便学习,还是自己写一个。可以更好的理解。

  • 配置文件
    就说想了很久还是没想好嘛,于是我就是开了一下thinkphp 函数方法。模仿写了一个函数。方便调用。
function C ($type) {	$file = YIN_PATH."/"."config/conf.php";	//echo $file;	$data = include $file;	return $data[$type];}

也可以Composer包中的配置类也可以。

namespace core\gzy;use core\lib\Route;use Noodlehaus\Config;class Gzy{	private $modules;       //默认模块	private $controller;    //默认控制器	private $action;        //默认方法	private $conf;          //配置文件	private $parm;	public function __construct() {		$this->conf = new Config(YIN_PATH. '/config/conf.php');	}
  • 路由、
    路由的话是比较复杂的。我们可以在框架里写自己的路由类,也可以直接去 nginx 里配置 rewrite 呀。你看,这样配置不是也挺好的。

然后在 nginx 里配置:

location / {    index index.php;    try_files $uri $uri/ /index.php$is_args$args;    #autoindex on;  }

apache配置 可以在项目文件.htaccess中写

# 打开Rerite功能 RewriteEngine On # 如果请求的是真实存在的文件或目录,直接访问 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # 如果访问的文件或目录不是真事存在,分发请求至 index.php RewriteRule . index.php

无论如何,路由就是如何根据 url 找到你所要访问的类和方法

首先建立目录 /gzy/lib 在 lib 目录里建立 Route.php 类.

conf = new Config(YIN_PATH. '/config/conf.php'); } /** * 路由的分发 */ public function dispatcher() { //echo $_SERVER['REQUEST_URI'];exit; switch ($this->conf->get('url_route')) { //斜杠模式 case 'PATH_INFO' : $request = $this->getRequest("PATH_INFO"); //var_dump($this->parsePathUri($request)); //$this->parsePathUri($request); break; default : $request = $this->getRequest(); break; } return $request; } /** * 获取controller和action */ private function getRequest($pathInfo=""){ if ($pathInfo == "PATH_INFO") { //var_dump($_SERVER); $pathInfo = trim($_SERVER["REQUEST_URI"],"/"); $parm = explode('/',$pathInfo); print_r($parm); if (trim($_SERVER["DOCUMENT_URI"],"/")=="index.php") { $this->modules = isset($parm[1]) ? $parm[1] : $this->conf->get('modules'); $this->controller = isset($parm[2]) ? $parm[2] : $this->conf->get('controller'); $this->action = isset($parm[3]) ? $parm[3] : $this->conf->get('action'); } else { $this->modules = isset($parm[0]) ? $parm[0] : $this->conf->get('modules'); $this->controller = isset($parm[1]) ? $parm[1] : $this->conf->get('controller'); $this->action = isset($parm[2]) ? $parm[2] : $this->conf->get('action'); } //print_r($parm) ;exit; //echo $this->is_url($pathInfo); } else { //var_dump($_SERVER); $this->modules = isset($_GET['g']) ? $_GET['g'] : $this->conf->get('modules'); $this->controller = isset($_GET['c']) ? $_GET['c'] : $this->conf->get('controller'); $this->action = isset($_GET['a']) ? $_GET['a'] : $this->conf->get('action'); } return [ "modules" => $this->modules, "controller" => $this->controller, "action" => $this->action, ]; } /** * @param $request */ public function parsePathUri($request){ }}

马上就要用到模块和控制器了,有了路由(其实本篇完全没具体说路由)根据 url 就能找到控制器了,我们先建立一个模块,然后控制器,然后再接着说如何找到他。

建立目录 /home/controller 然后在 controller 里建立 IndexController.php 类:

在这里插入图片描述
并且在 /config/conf.php 里加上默认控制器和方法配置

在这里插入图片描述

在类 gzy 里,我们原来建立了核心入口 run 方法:

在这里插入图片描述

这些完成之后,就可以访问了 这里提供了两种访问方式

  • get方式参数

在这里插入图片描述

pathinfo路径形式 要在conf.php 修改"url_route" => ‘PATH_INFO’, //PATH_INFO 普通模式

在这里插入图片描述

路由和配置就讲这么多了。本节结束。

转载地址:http://iscp.baihongyu.com/

你可能感兴趣的文章
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>