focus237 / the-router
简单的PHP路由器
Requires
- php: 7.*
This package is auto-updated.
Last update: 2024-09-21 20:00:31 UTC
README
安装
使用以下命令安装-the-router项目的最新版本。
composer require focus237/the-router
功能
- 基本路由(
GET
、POST
、RESOURCE
)。 - 参数的正则表达式约束。
- 命名路由。
- 生成路由的URL。
- 命名空间。
- 可选参数
- 子域路由
- 自定义启动管理器以将URL重写为“更友好”的URL。
- 输入管理器;轻松管理
GET
、POST
。
服务器设置
设置Apache
Apache无需特殊设置即可运行。我们在根目录中包含了.htaccess
文件。如果您发现重写不起作用,请检查Apache配置中是否已启用mod_rewrite
模块(htaccess支持)。
.htaccess示例
以下是-the-router使用的示例.htaccess
文件。
只需在您的根目录中创建一个新的.htaccess
文件,然后将以下内容粘贴到您新创建的文件中。这将重定向所有请求到您的index.php
文件。根据您的index.php
位于public
文件夹中,您将写下类似以下内容:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/index.php?url=$1 [QSA,L]
设置Nginx
如果您正在使用Nginx,请确保已启用URL重写。
您可以通过为demo-project的Nginx配置文件添加以下配置来轻松启用URL重写。
location / {
try_files $uri $uri/ /public/index.php?$query_string;
}
配置
创建一个新文件,命名为routes.php
并将其放置在您的库文件夹中。这将是一个文件,您在其中定义项目中所有路由。
警告:永远不要将您的routes.php
放置在公共文件夹中!
在您的index.php
中,要求您的新的routes.php
并调用$router->run()
方法。这将触发并执行实际的路由请求。
这不是必需的,但您可以设置TheRouter::setDefaultNamespace('\Controllers\Path');
以将命名空间前缀添加到所有路由。这将使事情更简单,因为您不需要在每次路由中指定控制器的命名空间。控制器的默认命名空间是App\Controller
。
以下是基本index.php
文件的示例
<?php
use TheRouter\Router\Router;
$router = new Router();
/* Load external routes file */
require_once 'routes.php';
/**
* The default namespace for route-callbacks, so we don't have to specify it each time.
* Can be overwritten by using the namespace config option on your routes.
*/
TheRouter::setDefaultNamespace('App\Controller');
/* Start the routing */
$router->run();
路由
请记住,在您的index.php
中要求的routes.php
文件?该文件将用于放置所有自定义路由规则。
基本路由
以下是设置路由的一个非常基本的示例。第一个参数是路由应匹配的URL - 下一个参数是Closure
或回调函数,一旦匹配到路由就会被触发。
$router->get('/', function() {
return 'Hello world';
});
或者您可以通过使用ControllerName@Function
来调用控制器操作
$router->get('/', 'DefaultController@index');
可用方法
在这里您可以看到所有可用路由的列表
$router->get($url, $callback, $name);
$router->post($url, $callback, $name);
$router->resource($name, $controller);
$name
是路由名称。有关命名路由的更多信息,请参阅命名路由部分。
路由参数
您可能会想知道如何从URL中解析参数。例如,您可能想要从URL中捕获用户ID。您可以通过定义路由参数来完成此操作。
$router->get('/user/:id', function ($id) {
return 'User with id : ' . $id;
});
您可以根据需要定义任意多的路由参数
$router->get('/posts/:post/comments/:comment', function ($post, $comment) {
// ...
});
正则表达式约束
您可以使用路由实例上的where方法来限制路由参数的格式。where方法接受参数名称和定义参数约束的正则表达式
$router->get('/user/:name', function ($name) {
// ... do stuff
})->where('name', '[A-Za-z]+');
$router->get('/user/:id', function ($id) {
// ... do stuff
})->where('id', '[0-9]+');
$router->get('/user/:id/:name', function ($id, $name) {
// ... do stuff
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
命名路由
命名路由允许方便地生成特定路由的URL或重定向。定义路由名称有两种方法。
链式调用
您可以通过在路由定义上链式调用name方法来指定路由名称(在这个例子中,profile
是路由名称)
$router->get('/user/profile', function () {
// Your code here...
})->name('profile');
名称参数
您可以通过在路由回调旁边添加名称来指定路由名称(在这个例子中,profile
是路由名称)
$router->get('/user/profile', function () {
// Your code here...
}, 'profile');
生成命名路由的URL
一旦为给定路由分配了名称,您可以使用全局函数$router->url('route_name')
在生成URL或重定向时使用该路由的名称
// Generating URLs...
$url = $router->url('profile');
如果命名路由定义了参数,您可以将参数作为url函数的第二个参数传递。给定的参数将自动按正确位置插入到URL中
$router->get('/user/:id/profile', function ($id) {
//
})->name('profile');
$url = $router->url('profile', ['id' => 1]);