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

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

掌握路由与配置:从零开始构建高效的Web应用

在开发过程中,路由和配置是构建高效Web应用的核心环节。本文将从路由的基本原理到配置的实现,逐步带你了解如何优化你的项目架构。

一、路由的核心概念

路由是Web应用中将URL映射到具体的资源(如控制器、方法等)的桥梁。在我们的项目中,我们可以通过自定义路由类来实现URL到逻辑的映射。

二、路由配置的实现

在项目根目录下,创建config/conf.php文件,配置默认的模块、控制器和方法:

// config/conf.phpreturn [    'url_route' => 'PATH_INFO',    'modules' => 'core',    'controller' => 'IndexController',    'action' => 'index'];

三、Nginx路由配置

.htaccess文件中启用Nginx的重写功能:

RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . index.php

四、自定义路由类

lib目录下创建Route.php,实现路由分发功能:

// core/lib/Route.phpnamespace core\lib;use Noodlehaus\Config;class Route{    private $conf;    public function __construct()    {        $this->conf = new Config(YIN_PATH . '/config/conf.php');    }    public function dispatcher()    {        switch ($this->conf->get('url_route')) {            case 'PATH_INFO':                $request = $this->getRequest('PATH_INFO');                break;            default:                $request = $this->getRequest();                break;        }        return $request;    }    private function getRequest($pathInfo = "")    {        $pathInfo = trim($_SERVER["REQUEST_URI"], "/");        $parm = explode('/', $pathInfo);        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');        }        return [            "modules" => $this->modules,            "controller" => $this->controller,            "action" => $this->action        ];    }}

五、模块化开发

创建/home/controller目录,并在controller目录下添加IndexController.php

// home/controller/Controller.phpnamespace core\controller;class IndexController{    public function index()    {        echo "欢迎访问我们的网站!";    }}

六、应用入口

gzy核心入口中定义run方法:

// core/gzy.phpnamespace core;use core\lib\Route;class Gzy{    public static function run()    {        $route = new Route();        $request = $route->dispatcher();        $modules = $request['modules'];        $controller = $request['controller'];        $action = $request['action'];        try {            $class = $modules . '\\' . $controller . 'Controller';            $instance = new $class();            $method = $action;            $instance->$method();        } catch (\Exception $e) {            echo "系统错误:" . $e->getMessage();        }    }}

七、访问方式

通过GET请求参数或PATH_INFO方式访问:

http://localhost?g=core&c=IndexController&a=index
http://localhost/index.php

八、路由优化

config/conf.php中设置url_route'PATH_INFO',以支持路径信息模式:

'url_route' => 'PATH_INFO'

九、总结

通过以上配置和实现,你已经掌握了路由和配置的核心知识。路由的灵魂在于将URL映射到具体的控制器和方法,而配置则为路由提供必要的支持。理解这些基础知识是构建高效Web应用的关键。

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

你可能感兴趣的文章
Openlayers高级交互(11/20):显示带箭头的线段轨迹,箭头居中
查看>>
Openlayers高级交互(14/20):汽车移动轨迹动画(开始、暂停、结束)
查看>>
Openlayers高级交互(15/20):显示海量多边形,10ms加载完成
查看>>
Openlayers高级交互(16/20):两个多边形的交集、差集、并集处理
查看>>
Openlayers高级交互(17/20):通过坐标显示多边形,计算出最大幅宽
查看>>
Openlayers高级交互(19/20): 地图上点击某处,列表中显示对应位置
查看>>
Openlayers高级交互(2/20):清除所有图层的有效方法
查看>>
Openlayers高级交互(20/20):超级数据聚合,页面不再混乱
查看>>
Openlayers高级交互(3/20):动态添加 layer 到 layerGroup,并动态删除
查看>>
Openlayers高级交互(6/20):绘制某点,判断它是否在一个电子围栏内
查看>>
Openlayers高级交互(7/20):点击某点弹出窗口,自动播放视频
查看>>
Openlayers高级交互(8/20):选取feature,平移feature
查看>>
Openlayers:DMS-DD坐标形式互相转换
查看>>
openlayers:圆孔相机根据卫星经度、纬度、高度、半径比例推算绘制地面的拍摄的区域
查看>>
OpenLDAP(2.4.3x)服务器搭建及配置说明
查看>>
OpenLDAP编译安装及配置
查看>>
Openmax IL (二)Android多媒体编解码Component
查看>>
OpenMCU(一):STM32F407 FreeRTOS移植
查看>>
OpenMCU(三):STM32F103 FreeRTOS移植
查看>>
OpenMCU(三):STM32F103 FreeRTOS移植
查看>>