basecode / route
组件路由,用于创建具有路由功能的 MVC 项目。
2.1.0
2022-03-11 15:48 UTC
Requires
- php: >=7.0
README
什么是 route ?
摘要:Route 是一个独立组件,用于在 MVC 系统中创建路由。
特性
- 创建快速路由。
- 创建带有动态参数的路由。
- 创建类型为 [ get | post | put | patch | delete ] 的路由。
- 默认路由创建。
入门
安装
您可以使用 composer 在项目中安装 route。
只需在终端运行以下命令
composer require basecode/route
或在您的 composer.json 中添加 require
"basecode/route": "2.1.*"
使用方法
安装 route 后,使用非常简单,只需在项目的 index 文件中实例化一个 route 对象,然后开始创建您的路由。
使用 route 的建议
建议您有一个 .htaccess 文件,将 URI 作为 GET[route] 参数指向项目的索引,其中包含路由。
示例 .htaccess 文件
RewriteEngine On Options -Indexes RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?route=$1
或者,您可以将接收路由作为参数的变量传递给 execute 方法,以下是一个示例。
示例 index.php 文件
define("DS", DIRECTORY_SEPARATOR); define("DOMAIN", "https:///project"); require_once dirname(__DIR__).DS."vendor".DS."autoload.php"; use BaseCode\Route\Route; /* domain_url [string] [required] separator_controller_method [string] [optional] [default] = ":" $route = new Route(domain_url, separator_controller_method); # EXAMPLE $route = new Route(DOMAIN, ":"); */ $route = new Route(DOMAIN); /* route [string] [required] route_action [string|function] [required] route_name [string] [optional] $route->get(route, route_action, route_name); $route->post(route, route_action, route_name); $route->put(route, route_action, route_name); $route->delete(route, route_action, route_name); action [string|function] $route->standard(action); # EXAMPLE $route->get("admin/login", function() { echo "<h1>ADMIN LOGIN</h1>"; }, "admin.login"); */ $route->get("/", function() { echo "<h1>HOME PAGE</h1>"; }, "home"); $route->namespace("Controllers\\Admin")->group("admin"); $route->get("/", "Controller:login", "admin.login"); // get route to namespace: Controllers\Admin\Controller - method: login $route->execute(); /* or $route->execute("route"); // route is the name received by $_GET */
创建标准路由
当通过 URL 请求的路由未找到时,使用标准路由。
示例 标准 路由
$route = new Route(DOMAIN); $route->get("/", function() { echo "<h1>HOME PAGE</h1>"; }, "home"); $route->standard(function($error) { http_response_code($error["code"]); echo "<h1>ERROR: ".$error["message"]."</h1>"; }); $route->execute();
使用 route 方法
route 方法返回指定名称的路由,如果未找到则返回 null。
示例 route 方法
$route = new Route(DOMAIN); $route->get("/", function() use ($route) { $link = $route->route("landing.page"); echo "<h1>HOME PAGE</h1><br><a href=\"{$link}\">Landing</a>"; }, "home"); $route->get("landing", function() use ($route) { $link = $route->route("home"); echo "<h1>LANDING PAGE</h1><br><a href=\"{$link}\">Back Home</a>"; }, "landing.page"); $route->execute();
向路由传递参数
可以为路由定义参数,从而允许接收动态值。
示例 参数 为路由
$route = new Route(DOMAIN); $route->get("hello/{name}", function($data) { $name = $data["name"]; echo "<h1>HELLO {$name}</h1>"; }, "hello.page"); $route->execute();
获取当前路由的 URL
可以使用 (current) 方法获取当前路由的 URL。
示例 当前路由
$route = new Route(DOMAIN); $route->get("product/{id}", function() use ($route) { $current = $route->current(); echo "<p>ROUTE: {$current}</p>"; }, "product.page"); $route->execute();
路由重定向
可以使用 (redirect) 方法重定向路由。
示例 重定向
$route = new Route(DOMAIN); // REDIRECT OPTIONS /* USING NAME ROUTES EXAMPLES ( $route->get("/home", route_action, "page.home"); $route->get("/product/{id}", route_action, "page.product"); ) $route->redirect("page.home"); // redirect to domain/home $route->redirect("page.product", ["id" => 10]); // redirect to domain/product/10 */ /* USING URL $route->redirect("https://www.google.com"); // redirect to passed URL */ $route->group("admin"); $route->get("/login", function() { echo "<h1>ADMIN LOGIN</h1>"; }, "admin.login"); $route->get("/home", function() use ($route) { if (!isset($_SESSION["ADMIN_USER"])) { $route->redirect("admin.login"); } echo "<h1>ADMIN HOME</h1>"; }, "admin.home"); $route->execute();
待续...