laralike/laralike-router

该软件包最新版本(v1.0.4)没有可用的许可证信息。

类似于Laravel的简单路由器

v1.0.4 2020-07-18 08:59 UTC

README

我制作了一个类似于Laravel的独立运行的路由库。

考虑到极小规模的开发,假设不会使用框架。

可以使用composer进行安装。需要PHP版本7.1以上。

GitHub: kamiya-kei/LaralikeRouter

安装方法

方法1:使用composer

composer require laralike/laralike-router
<?php

use \laralike\LaralikeRouter as Route;

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

Route::get('/', function () { echo 'hello milky'; });

使用.htaccess等,将所有访问重定向到index.php

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

用法

大多数用法与Laravel的路由相同。但无法使用命名路由、子域名路由、模型绑定路由等部分功能。 示例: LaralikeRouter/tests/index.php

基本路由

第一个参数以/开头或不开头,效果相同。

Route::get('/', function () { return 'hello milky'; });

Route::get('/foo', function () { return 'FOO'; });
Route::get('bar', function () { return 'BAR'; });

// returnされた値がarray型の場合は、
// `Content-Type: application/json`ヘッダを出力して、戻り値をjson_encodeして出力します。
Route::get('baz', function () { return ['BAZ']; });

使用控制器时,可以这样操作。

Route::get('/', '\App\Controller\TestController@index');

省略时,可以这样设置控制器的namespace。 默认情况下设置为App\Http\Controllers\

Route::setNamespace('\\App\\Controller\\');
Route::get('/', 'TestController@index');

可以使用与Laravel相同的方法定义HTTP方法。

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

Route::match(['get', 'post'], '/', $callback);
Route::any('/', $callback);

重定向路由

可以使用与Laravel相同的方法定义重定向路由。

Route::redirect('/here', '/there'); // 302
Route::redirect('/here', '/there', 301);
Route::permanentRedirect('/here', '/there'); // 301

Laravel没有的语法,但也可以这样使用。

Route::get('/here', function () { Route::runRedirect('/there'); }); // 302
Route::get('/here', function () { Route::runRedirect('/there', 301); });

在路由组内使用时,可以这样操作。

Route::prefix('/foo')->group(function () {
	Route::redirect('/here', '/there'); // `/foo/here` => `/foo/there`
});

视图路由

要定义视图路由,请自行引入喜欢的模板引擎库,并按照以下方式设置(以下为twig的设置示例)。

function view ($viewfile, $parameters) {
  $loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/');
  $twig = new \Twig\Environment($loader, [
    'debug' => true
  ]);
  return $twig->render($viewfile, $parameters);
}
Route::setView(view:class);
Route::prefix('/view')->group(function () {
  // 以下の様に使用するなら`Route::setView`は不要です。
  Route::get('/twig1', function () { return view('test.html.twig', ['name' => 'lala']); });
  // 以下の様ビュールートを使用する場合は`Route::setView`で予め設定する必要がある。
  Route::view('/twig2', 'test.html.twig', ['name' => 'milky']);
});

路由参数

可以使用与Laravel相同的方法定义路由参数。

默认情况下,除/外,允许所有字符。

必填参数

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});

可选参数

Route::get('user/{name?}', function ($name = null) {
    return $name;
});

Route::get('user/{name?}', function ($name = 'lala') {
    return $name;
});

正则表达式约束

正则表达式约束可以这样定义。where参数必须传递关联数组。

Route::get('user/{id}', function ($id) {
    //
})->where(['id' => '[0-9]+']);
// NG: ->where('id', '[0-9]+');

Route::get('user/{id}/{name}', function ($id) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

路由组

路由组可以这样定义。

也支持嵌套。

Route::prefix('/foo')->group(function () {
    // `/foo/bar`で`FOOBAR`と出力される
	Route::get('/bar', function () { echo 'FOOBAR' });
});

Route::middleware([function () { echo 'Hello '; }])
  ->prefix('/hello')
  ->group(function () {
    // `hello/lala`で`Hello lala`と出力される
    Route::get('/lala', function () { echo 'lala'; });
    // `hello/hikaru`で`Hello hikaru`と出力される
    Route::get('/hikaru', function () { echo 'hikaru'; });
});

// ネスト
Route::prefix('/hello')
  ->middleware([function () { echo 'Hello '; }])
  ->group(function () {
    Route::prefix('/cure')
      ->middleware([function () { echo 'Cure '; }])
      ->group(function () {
          // `hello/cure/milky`で`Hello Cure Milky`と出力される
          Route::get('/milky', function () { echo 'Milky'; });
      });
  });

备用路由(404 Not Found)

可以使用与Laravel相同的方法定义所有路由不匹配时的处理。

Route::fallback(function () {
  // 404
});

如果未定义,则自动调用Route::defaultFallback()

defaultFallback的内容如下。

  public static function defaultFallback()
  {
    header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
    echo '404 Not Found';
  }