webservis / php-router
简单的PHP路由器示例
dev-main
2024-03-05 08:31 UTC
Requires
- php: >=7.4
This package is auto-updated.
Last update: 2024-09-05 09:27:01 UTC
README
这是一个简单的PHP路由器类,可用于为Web应用程序定义路由。
用法
-
安装
首先,您需要在项目中包含
Route.php
文件。您可以通过将Route.php
文件的内容复制到您的项目中或使用Composer来完成此操作。composer require webservis/php-router
-
基本用法
<?php require_once 'vendor/autoload.php'; // Adjust this based on your project's structure use Webservis\Route; $router = new Route(); $router->get('/', function () { echo "Hello, World!"; }); $router->get('/about', function () { echo "About Us Page"; }); $router->dispatch();
-
定义路由
$router->get('/products/:id', function ($id) { echo "Product ID: $id"; })->name('product'); $router->get('/categories/:slug', 'CategoryController@show')->name('category.show');
-
URL生成
$productUrl = $router->url('product', ['id' => 123]); echo "Product URL: $productUrl";
-
子域名路由
$router->subdomain('admin', function () { Route::get('/dashboard', 'AdminController@dashboard')->name('admin.dashboard'); });
-
前缀路由
Route::prefix('/admin/auth')->group(function (){ Route::get('/?', 'Auth@index')->name('auth'); Route::get('/login', 'Auth@login')->name('auth/login'); Route::get('/logout','Auth@logout')->name('auth/logout'); //Route::get('/auth',function(){return 'This page is Authentification page';})->name('auth'); Route::redirect('/auth/signin','/auth/login'); });
-
阅读更多
有关更多详细信息和高阶用法,请参阅
docs
目录中的文档。