webservis / php-router

简单的PHP路由器示例

dev-main 2024-03-05 08:31 UTC

This package is auto-updated.

Last update: 2024-09-05 09:27:01 UTC


README

这是一个简单的PHP路由器类,可用于为Web应用程序定义路由。

用法

  1. 安装

    首先,您需要在项目中包含Route.php文件。您可以通过将Route.php文件的内容复制到您的项目中或使用Composer来完成此操作。

    composer require webservis/php-router
  2. 基本用法

    <?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();
  3. 定义路由

    $router->get('/products/:id', function ($id) {
        echo "Product ID: $id";
    })->name('product');
    
    $router->get('/categories/:slug', 'CategoryController@show')->name('category.show');
  4. URL生成

    $productUrl = $router->url('product', ['id' => 123]);
    echo "Product URL: $productUrl";
  5. 子域名路由

    $router->subdomain('admin', function () {
        Route::get('/dashboard', 'AdminController@dashboard')->name('admin.dashboard');
    });
  6. 前缀路由

    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');
    });
  7. 阅读更多

    有关更多详细信息和高阶用法,请参阅docs目录中的文档。