博客
关于我
怎么样写一个自己的渣渣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/

你可能感兴趣的文章
NMF(非负矩阵分解)
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>