php 框架 漂亮,PHP框架中的漂亮URL

这通常通过将所有请求路由到单个入口点(基于请求执行不同代码的文件)来完成,其规则如下:

# Redirect everything that doesn't match a directory or file to index.php

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule .* index.php [L]然后,此文件将请求($_SERVER["REQUEST_URI"])与路由列表进行比较 - 将请求匹配的模式映射到控制器操作(在MVC应用程序中)或另一个执行路径。框架通常包括可以从请求本身推断控制器和操作的路由,作为备用路由。

一个简单的小例子:

// Define a couple of simple actions

class 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;结合第一个配置,这是一个简单的脚本,允许您使用domain.com/about等URL。希望这可以帮助您了解这里发生的事情。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值