vdauchy/laravel-routing-extender

0.2.0 2021-03-20 22:29 UTC

This package is auto-updated.

Last update: 2024-09-21 06:33:48 UTC


README

Laravel 路由扩展器

此包扩展了 Laravel 的核心路由服务。

  • 使用宏 optionalParameterGroup 添加对路由中间部分的可选参数的支持。
// In Routes/web.php:
Route::optionalParameterGroup('hreflang', '^[a-z]{2}(?:\-[a-z]{2})?$', ['as' => 'Hreflang::'], function () {
    Route::get('home')->name('home');
});

// In YourCode.php:
route('Hreflang::home', []); // Will return `/home`
route('Hreflang::home', ['hreflang' => 'en-us']); // Will return `/en-us/home`
  • 通过添加宏 customRouteResolver 在 UrlGenerator 中添加对自定义路由解析器的支持。
// In xxxProvider.php
URL::macro('customRouteResolver', function($name, $parameters, $absolute): ?\Illuminate\Routing\Route {
    if ($name instanceof CustomClassA::class) {
        return $name->getRoute();
    }
    if ($name instanceof CustomClassB::class) {
        return $name->generateRoute();
    }
    return null;
});

// In YourCode.php:
route($instanceClassA); // Will generate the url from the Route object return by `$instanceClassA->getRoute()`
route($instanceClassB); // Will generate the url from the Route object return by `$instanceClassB->generateRoute()`