bluedragon/laravel-routes

将Laravel路由发布到JavaScript

2.10.0 2024-03-11 11:42 UTC

This package is auto-updated.

Last update: 2024-09-11 12:35:59 UTC


README

Latest Stable Version pipeline status coverage report License

Laravel Routes为您提供了一个灵活的包,使Laravel路由在JavaScript中可用。使用Laravel Routes,您可以按组划分路由,并只加载JavaScript中需要的路由。您完全控制。

快速开始

  • 运行 composer require bluedragon/laravel-routes --dev
  • laroute_publish => true添加到您希望在JavaScript中访问的路由中。
    Route::get('/', ['uses' => function () {
      return view('welcome');
    }, 'laroute_publish' => true])
    ->name('home');
    
  • 运行 php artisan laravel-routes:publishall
  • 如果您尚未运行php artisan storage:link
  • 将以下部分添加到您的html中。
    <script type="text/javascript" src="{{ asset('/storage/laravel_routes/laravel_routes.js') }}"></script>
    
  • 通过以下函数加载Laravel Routes文件
    const laravelRoutes = new LaravelRoutes('{{ asset('/storage/laravel_routes/routes.json')  }}');
    
  • 您可以通过以下方式检索路由
    laravelRoutes.route('route.name', {parameterName: parameterValue});
    

安装

Laravel Routes通过composer提供。通过执行:composer require bluedragon/laravel-routes --dev 安装Laravel Routes。我们建议将该包作为开发依赖项安装。在生产环境中不需要Laravel Routes包,因为所有文件将在开发期间生成。

自动提供者

默认情况下,此包将自动发现。如果您已禁用自动发现,则需要将BlueDragon\LaravelRoutes\LaravelRoutesServiceProvider::class添加到config/app.php中的提供者数组中,并确保它仅在开发期间加载。

配置

要获得所有配置选项,可以使用php artisan vendor:publish发布配置文件。配置文件已注释并具有自解释性。

配置要发布的路由

请注意,没有名称的路由永远不会发布。

路由文件

发布路由有多种方式。最简单的方法是将'laroute_publish' => true添加到路由中。

Route::get('/', ['uses' => function () {
    return view('welcome');
}, 'laroute_publish' => true])
->name('home');

还可以在routesgroup上使用'laroute_publish' => true。在组内,您可以根据以下示例覆盖组内特定路由的值。

Route::group(['laroute_publish' => true], function () {
    Route::get('/', function () {
        return view('welcome');
    })->name('home');

    Route::get('/hidden', ['uses' => function () {
        return view('welcome');
    }, 'laroute_publish' => false])
    ->name('hidden');

    Route::get('/contact', function () {
        return view('contact');
    })->name('contact');
});

在配置文件中

第三种方法是定义配置文件中的路由。任何设置都将覆盖配置值。定义是在路由组中完成的,默认情况下我们使用默认组。

//config/laravel-routes.php
    'route_groups' => [
        'default' => [ // the group name
            'include_true' => true, // if we include the routes with 'laroute_publish' => true
            'routes' => [ // the routes we want to add to the group
                'home' => true,
                'resource.show' => true,
            ],
        ],
    ],

路由组

特别是在大型项目中,将要在JavaScript中使用的路由拆分为组可能很有用。如果您有一个具有CMS(例如)的web应用程序,您可以决定将路由拆分为组。这样,CMS路由将仅在您的web应用程序的CMS部分中加载,并排除前端路由。

有多种方式可以向组添加路由。通过将它们添加到自己的组中的配置文件中,可以将路由添加到组中。

//config/laravel-routes.php
    'route_groups' => [
        'default' => [ // the group name
            'include_true' => true, // if we include the routes with 'laroute_publish' => true
            'routes' => [ // the routes we want to add to the group
            ],
        ],
        'cms' => [ // the group name
            'include_true' => false, // if we include the routes with 'laroute_publish' => true
            'routes' => [ // the routes we want to add to the group
                'cms.pages.index' => true,
                'cms.pages.create' => true,
                'cms.pages.store' => true,
                '...' => true,
            ],
        ],
    ],

另一种方法是在路由文件(group)中定义组名

Route::get('/cms/pages', ['uses' => function () {
    return view('cms.pages.index');
}, 'laroute_publish' => 'cms'])
->name('cms.pages.index');

如果您在配置文件中定义了多个route_groups,并且想要访问两个组中的路由,则在配置文件中将include_true设置为true。然后在路由文件中为要访问两个组中的路由添加laroute_publish' => true

不需要在配置文件中定义组。如果没有定义组,则在执行laravel-routes:publishall命令时不会发布路由。

更改文件位置。

默认情况下,文件存储在public/laravel_routes/目录下,这可以通过配置文件进行更改。更改路径的选项是:export_directory

命令

该包提供了三个命令。

Artisan 命令函数
laravel-routes:publishscript {--filename=} {--path=}这会发布带有JavaScript函数的文件。
laravel-routes:publishroutes {--filename=} {--path=} {--group=}这会发布某个组的路由,默认为默认组
laravel-routes:publishall {--path=}这会发布配置中所有组的JavaScript函数文件和路由文件。

替代方案

如果您使用的是较旧的Laravel版本,可以使用lord/laroute。Laroute是这个包的灵感来源。其他可能有用的类似包可能包括

从lord/laroute迁移

如果您目前正在使用lord/laroute,您可以通过一些小的配置更改使用Laravel Routes包。在发布配置文件后,您需要将 'route_keyname' => 'laroute_publish' 更改为 'route_keyname' => 'laroute'。这样,您就不必更改所有路由。与lord/laroute相比,我们明确不支持默认发布所有路由的选项。如果您想要默认发布所有路由,您需要将所有路由放置在一个路由组中。

Route::group(['laroute' => true], function () {
    // everything from your routes file
});

对于迁移的JavaScript部分,我们可以通过以下代码加载路由文件

const laroute = new LaravelRoutes('{{ asset('/storage/laravel_routes/routes.json')  }}');

要获取一个路由,可以调用以下函数

laroute.route('route.name', {parameterName: parameterValue});

更新日志

我们(尝试)在CHANGELOG中记录了所有更改。请查阅以获取更多信息。

贡献

非常欢迎您贡献力量,请在CONTRIBUTING中了解相关信息。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

由蓝龙制作

这个包是由蓝龙数字技术开发的。成立于1999年,蓝龙数字技术一直提供领先的数字解决方案,从网络应用程序和电子商务解决方案到移动应用程序。我们是蓝龙家族的一部分。您会在创意、技术和连接的交汇处找到我们。在这里,一切皆有可能,以帮助沟通产生影响。

Blue Dragon