kaio-souza/php-router

一个简单的PHP路由器

0.01 2022-05-22 06:36 UTC

This package is auto-updated.

Last update: 2024-09-23 00:25:23 UTC


README

一个简单的PHP路由器

如何配置环境

要使用路由器,您需要将所有请求重写到一个文件中,在该文件中创建路由类的一个实例。

Apache .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

Nginx nginx.conf

try_files $uri /index.php;

如何运行示例

在项目文件夹中运行

php -S localhost:8080 -t ./example

使用路由器

实例化路由器

$router = new Router();

创建路由

$router->get('/xpto', function(){
    echo 'Hello World';
});

创建带参数的路由

$router->get('/xpto/{param}', function($param){
    echo $param;
});

监听请求

将其添加到文件末尾

$router->listen();