PHPackages                             ljw/route - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. ljw/route

ActiveLibrary[API Development](/categories/api)

ljw/route
=========

a simple route

v4.0(3y ago)11091MITPHPPHP &gt;=7.0

Since Apr 26Pushed 3y ago1 watchersCompare

[ Source](https://github.com/lejianwen/route)[ Packagist](https://packagist.org/packages/ljw/route)[ Docs](https://github.com/lejianwen/route)[ RSS](/packages/ljw-route/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (12)Used By (1)

一个简单的路由工具
=========

[](#一个简单的路由工具)

- 项目地址

安装
--

[](#安装)

```
composer require "ljw/route": "dev-master"

```

使用方式
----

[](#使用方式)

```
use \Ljw\Route\Route;
// 第一个参数是uri， 最后一个是controller， 中间的都为中间件
Route::$method($uri, $controller);
Route::$method($uri, $middleware, $controller);
```

基本使用
----

[](#基本使用)

```
use \Ljw\Route\Route;

Route::get('', 'app\\controllers\\IndexController@index');

Route::post('index/test', 'app\\middleware\\Index', 'app\\controllers\\IndexController@test');

Route::post('index/test2/(:str)/(:num)', 'app\\middleware\\Index', 'app\\controllers\\IndexController@test2');
Route::post('index/test2/(:str)/(:num)',
function($params, $next)
{
    // todo 前置方法
    $next($params); // 如果这个不写则在此处中断
    // todo 后置方法
}, 'app\\controllers\\IndexController@test2');
```

### 1.中间件如果是类要实现Middleware接口，或者有handle方法

[](#1中间件如果是类要实现middleware接口或者有handle方法)

```
namespace app\middleware;

use Ljw\Route\Middleware;

class Index implements Middleware
{
    public function handle($params, $next)
    {
        // todo 前置方法
        $next($params); // 如果这个不写则在此处中断
        // todo 后置方法
    }
}
```

### 2.如果中间是闭包则和handle方法一样，需要接收2个参数

[](#2如果中间是闭包则和handle方法一样需要接收2个参数)

```
function($params, $next)
{
        // todo 前置方法
        $next($params); // 如果这个不写则在此处中断
        // todo 后置方法
}
```

### 3.控制器中可通过参数获取到匹配的值

[](#3控制器中可通过参数获取到匹配的值)

```
namespace app\controllers;

class IndexController
{
    public function test()
    {
        echo 'ok';
    }

    public function test2($str, $num)
    {
        var_dump($str, $num);
    }
}
```

命名空间自定义
-------

[](#命名空间自定义)

### 如果传入的中间件和控制是字符串， 则会自动加上命名空间

[](#如果传入的中间件和控制是字符串-则会自动加上命名空间)

```
use \Ljw\Route\Route;
//定义控制器和中间件的命名空间
Route::space('app\\controllers\\', 'app\\middleware\\');

Route::get('', 'IndexController@index');

Route::post('index/test', 'Index@test', 'IndexController@test');
```

通过中间件批量添加路由
-----------

[](#通过中间件批量添加路由)

```
use \Ljw\Route\Route;

Route::middleware('app\\middleware\\Index',
    function () {
        Route::get('/',  'IndexController@test');
    }
);
//定义控制器和中间件的命名空间
Route::space('app\\controllers\\', 'app\\middleware\\');
Route::middleware(['Index', 'Index'],
    function () {
        Route::get('/',  'IndexController@test');
    }
);
Route::middleware('Index',    function () {
        Route::get('/',  'IndexController@test');
    }
);
```

使用闭包
----

[](#使用闭包)

```
use \Ljw\Route\Route;
//定义控制器和中间件的命名空间
Route::get('', function (){
    echo 'index';
});

Route::get('index/index/(:str)/(:num)',function($params, $next)
                                       {
                                               // todo 前置方法
                                               $next($params); // 如果这个不写则在此处中断
                                               // todo 后置方法
                                       },
 function ($str, $num) {
    var_dump($str, $num);    //结果为 middle
});
```

错误处理
----

[](#错误处理)

```
use \Ljw\Route\Route;
//错误处理
Route::error(function (){
     http_response_code(404);
     echo '404 Not Found!';
});
//或者
Route::error('app\\controllers\\IndexController@error');
```

使用正则匹配
------

[](#使用正则匹配)

```
//正则匹配支持的方式
 $patterns = [
        ':any' => '[^/]+',
        ':str' => '[0-9a-zA-Z_]+',
        ':num' => '[0-9]+',
        ':all' => '.*'
 ];
// 也可以通过\Ljw\Route\Route::$patterns, 自行添加和修改
use \Ljw\Route\Route;
Route::space('app\\controllers\\', 'app\\middleware\\');
Route::get('index/(:num)', 'Index', 'IndexController@test');
```

```
//中间件
namespace app\middleware;

use Ljw\Route\Middleware;

class Index implements Middleware
{
    public function handle($params, $next)
    {
        // todo 前置方法
        $params[0]++;
        $next($params); // 如果这个不写则在此处中断
        // todo 后置方法
    }
}
```

```
//控制器
class IndexController{
    public function test($num)
    {
        echo $num;
    }
}
//GET /index/5 结果为6
```

TIPS
====

[](#tips)

1. 路由只有在匹配不到的时候才会匹配正则,正则如果也匹配不到则会调用error

```
use \Ljw\Route\Route;
Route::get('index/test', 'IndexController@test');
Route::get('index/(:str)', 'IndexController@index');
//GET /index/test时，会匹配第一条
```

2. 相同的路由规则,后面的路由会覆盖前面的

```
use \Ljw\Route\Route;
Route::get('index/test', 'IndexController@test');
Route::get('index/test', 'IndexController@index');
//GET /index/test时，会匹配第二条
```

3. 方式any中的路由优先级低于get,post等具体方式

```
use \Ljw\Route\Route;
Route::any('index/test', 'IndexController@any');
Route::get('index/test', 'IndexController@test');
Route::any('index/test', 'IndexController@index');
//GET /index/test时，会匹配第二条
//POST /index/test时，会匹配第三条
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~222 days

Recently: every ~338 days

Total

10

Last Release

1309d ago

Major Versions

v1.0 → v2.22018-02-12

v2.6 → 3.02020-03-07

v3.2 → v4.02022-10-14

PHP version history (2 changes)v1.0PHP &gt;=5.6

3.0PHP &gt;=7.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15119474?v=4)[乐](/maintainers/lejianwen)[@lejianwen](https://github.com/lejianwen)

---

Top Contributors

[![lejianwen](https://avatars.githubusercontent.com/u/15119474?v=4)](https://github.com/lejianwen "lejianwen (22 commits)")

---

Tags

phprouterouteljw

### Embed Badge

![Health badge](/badges/ljw-route/health.svg)

```
[![Health](https://phpackages.com/badges/ljw-route/health.svg)](https://phpackages.com/packages/ljw-route)
```

###  Alternatives

[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[nezamy/route

Route - Fast, flexible routing for PHP, enabling you to quickly and easily build RESTful web applications.

21436.6k5](/packages/nezamy-route)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
