栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP框架中的漂亮URL

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP框架中的漂亮URL

通常,通过使用以下规则将所有请求路由到单个入口点(根据该请求执行不同代码的文件)来完成:

# Redirect everything that doesn't match a directory or file to index.phpRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule .* index.php [L]

然后,该文件将请求(

$_SERVER["REQUEST_URI"]
)与路由列表进行比较-
匹配请求的模式到控制器动作(在MVC应用程序中)或其他执行路径的映射。框架通常包含一条路由,该路由可以从请求本身推断出控制器和操作,作为备用路由。

一个小的简化示例:

<?php// Define a couple of simple actionsclass Home {    public function GET() { return 'Homepage'; }}class about {    public function GET() { return 'about page'; }}// Mapping of request pattern (URL) to action classes (above)$routes = array(    '/' => 'Home',    '/about' => 'about');// Match the request to a route (find the first matching URL in routes)$request = '/' . trim($_SERVER['REQUEST_URI'], '/');$route = null;foreach ($routes as $pattern => $class) {    if ($pattern == $request) {        $route = $class;        break;    }}// If no route matched, or class for route not found (404)if (is_null($route) || !class_exists($route)) {    header('HTTP/1.1 404 Not Found');    echo 'Page not found';    exit(1);}// If method not found in action class, send a 405 (e.g. Home::POST())if (!method_exists($route, $_SERVER["REQUEST_METHOD"])) {    header('HTTP/1.1 405 Method not allowed');    echo 'Method not allowed';    exit(1);}// Otherwise, return the result of the action$action = new $route;$result = call_user_func(array($action, $_SERVER["REQUEST_METHOD"]));echo $result;

与第一个配置结合使用,这是一个简单的脚本,可让您使用像这样的URL

domain.com/about
。希望这可以帮助您了解这里发生的事情。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号