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

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

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

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

一、路由的核心概念

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

二、路由配置的实现

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

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

三、Nginx路由配置

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

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

四、自定义路由类

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

// core/lib/Route.php
namespace 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.php
namespace core\controller;
class IndexController
{
public function index()
{
echo "欢迎访问我们的网站!";
}
}

六、应用入口

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

// core/gzy.php
namespace 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/

你可能感兴趣的文章
NLP的神经网络训练的新模式
查看>>
NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
查看>>
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
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?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
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 ...的解决方法
查看>>