定制 artkoder/peasy-router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

artkoder/peasy-router

最新稳定版本:v0.1.1

Composer 安装命令:

composer require artkoder/peasy-router

包简介

A simple, easy to use, and opinionated router for PHP

README 文档

README

Peasy Router is an easy peasy and opinionated router for PHP. It is meant to be used with small projects that need a very basic ruting functionality. Nothing complicated.

Installation

Using composer

composer require artkoder/peasy-router

Configure your .htaccess file

Add the following code to your .htaccess inside a public folder at the same level as your index.php.

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Setting index.php

// public/index.php
require_once __DIR__ . '/../vendor/autoload.php';

use ArtKoder\Peasy\Http\Router;

// You need to pass the path to your routes directory
$router = new Router('/path/to/routes/directory');

$router->handleRequest();

Routes

You can define your routes inside a directory. You can use either a text file with extension .routes or a normal .php file. Peasy router will scan all the route files inside the directory recursivelly. You can use either of the file formats or combine both if you so desire.

Defining your routes using a .routes file

Here is a sample web.routes file:

name: budget-report
path: /budget/{start}/{end}
method: get
controller: ArtKoder\Peasy\Controllers\Budget::report

name: budget-index
path: /budget
method: get
controller: ArtKoder\Peasy\Controllers\Budget::index

Defining your routes using a .php file

The previous routes can alternatively be defined in a .php file as follows:

// web.php
return [
    [
        'name' => 'budget-report',
        'path' => '/budget/{start}/{end}',
        'method' => 'get',
        'controller' => 'ArtKoder\Peasy\Controllers\Budget::report'
    ],
    [
        'name' => 'budget-index',
        'path' => '/budget',
        'method' => 'get',
        'controller' => 'ArtKoder\Peasy\Controllers\Budget::index'
    ]
];

Controllers

Once you have your route definitions, you can create the controllers for your paths.

Here is an example using the previous route definitions:

// src/Controllers/Budget.php

namespace ArtKoder\Peasy\Controllers;

class Budget 
{
    public static function report($start, $end)
    {
        echo "start: $start\n";
        echo "end: $end\n";
    }

    public static function index()
    {
        echo "Budget index\n";
    }
}

Query parameters

Peasy router maps all query parameters as arguments to the controllers function. So, if you request the following path: https://peasy.test/budget/2023-09-01/2023-12-31?var=value, the controller will expect to get a variable $var, so you need to declare it. To avoid errors when a query parameter is not present, you can make variables optional.

Here is an example:

// src/Controllers/Budget.php

namespace ArtKoder\Peasy\Controllers;

class Budget 
{
    public static function report($start, $end, $var = '')
    {
        echo "start: $start\n";
        echo "end: $end\n";
        echo "var: $var\n";
    }

    public static function index()
    {
        echo "Budget index\n";
    }
}

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-23