定制 bluedragon/laravel-routes 二次开发

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

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

bluedragon/laravel-routes

Composer 安装命令:

composer require bluedragon/laravel-routes

包简介

Publish Laravel routes to JavaScript

README 文档

README

Latest Stable Version pipeline status coverage report License

Laravel Routes offers you a flexible package to make Laravel routes available in JavaScript. With Laravel Routes you are able to split routes into groups and only load the routes needed in JavaScript. You are in control.

Quick start

  • Run composer require bluedragon/laravel-routes --dev
  • Add laroute_publish => true to the routes you want to be able to access in JavaScript.
    Route::get('/', ['uses' => function () {
      return view('welcome');
    }, 'laroute_publish' => true])
    ->name('home');
    
  • Run php artisan laravel-routes:publishall.
  • If you haven't already run php artisan storage:link.
  • Add the next part to your html.
    <script type="text/javascript" src="{{ asset('/storage/laravel_routes/laravel_routes.js') }}"></script>
    
  • Load the Laravel Routes file via the following function:
    const laravelRoutes = new LaravelRoutes('{{ asset('/storage/laravel_routes/routes.json')  }}');
    
  • You can retrieve a route via:
    laravelRoutes.route('route.name', {parameterName: parameterValue});
    

Installation

Laravel Routes is available via composer. Install Laravel Routes by executing: composer require bluedragon/laravel-routes --dev. We suggest installing this package as a dev dependency. The Laravel Routes package is not needed in the production environment because all files will generated during development.

Auto providers

By default this package will be auto discovered. If you have disabled autodiscovery you need to add BlueDragon\LaravelRoutes\LaravelRoutesServiceProvider::class, to the providers array in config/app.php and make sure it is only loaded during development.

Configuration

To have all configuration options available, you can publish the config file with php artisan vendor:publish. The config file is is commented and self-explanatory.

Configurate the routes to publish.

Please note that a route without a name will never be published.

Routes file

There are different ways to publish a route. The easiest way is to add 'laroute_publish' => true to the route.

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

It is also possible to use ‘laroute_publish’ => true on a routesgroup. Within a group you are able to overwrite the value for a specific route within the group as shown below.

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');
});

In config file

The third option is to define the routes in a config file. Any setting in the routes file will overwrite the config value. The definition is done in route groups, by default we use the default group.

//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,
            ],
        ],
    ],

Route groups

Especially in bigger projects it can be usefull to split the routes you want to use in JavaScript into groups. If you have a webapplication with e.g. a CMS, you can decide to split the routes into groups. This way, the CMS routes will only load in the CMS part of your webapplication and are excluded from the frontend routes.

There are several ways to add routes to groups. Adding routes to groups can be done by adding them to the config file in their own group.

//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,
            ],
        ],
    ],

Another way is to define the groupname on the route (group) in the routes file

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

If you have multiple route_groups defined in the config and you want to access a route in both groups, in the config file set include_true to true. And in the routes file add laroute_publish' => true to the route that you want to access in both groups.

It is not needed to define a group in the config file. If there is no group defined the routes will not be published upon executing the laravel-routes:publishall command.

Change the location of the files.

By default the files are stored public/laravel_routes/, this can be changed with in the config file. The option to change the path is: export_directory.

Commands

We have three commands available with this package.

Artisan commandfunction
laravel-routes:publishscript {--filename=} {--path=}This publish the file with the javascript functions.
laravel-routes:publishroutes {--filename=} {--path=} {--group=}This publish the routes for a group, by default the default group
laravel-routes:publishall {--path=}This publish the javascript functions file and the route files for all the groups that are in the config.

Alternatives

If you have an older Laravel version you can use lord/laroute. Laroute is the package that inspired to create this one. Other possibly useful similar packages could be:

Migrating from lord/laroute

If you are currently using lord/laroute you can use the Laravel Routes package with some minor configuration changes. After publishing the config file you need to change 'route_keyname' => 'laroute_publish', to 'route_keyname' => 'laroute',. This way you don't have to change all the routes.In contrast to lord/laroute we explicitly don't support the option to publish all routes by default. If you want to publish all routes by default, you need to place all the routes in a route group.

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

For the JavaScript part of the migration we can load a routes file via the following code:

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

To retrieve a route the following function can be called:

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

Changelog

We (try to) document all the changes in CHANGELOG. Please consult it for more information.

Contributing

You are very welcome to contribute, read about it in CONTRIBUTING

License

The MIT License (MIT). Please see License File for more information.

Made by Blue Dragon

This package is developed by Blue Dragon Digital Technology. Established in 1999, Blue Dragon Digital Technology has been providing leading digital solutions ranging from webapplications and ecommerce solutions to mobile apps. We are part of the Blue Dragon family. You’ll find us where creativity, technology and connectivity intersect. Where everything, and we mean everything, is possible to help communication make an impact.

Blue Dragon

bluedragon/laravel-routes 适用场景与选型建议

bluedragon/laravel-routes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 92.56k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2019 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「routes」 「laravel」 「Blue Dragon」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bluedragon/laravel-routes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bluedragon/laravel-routes 我们能提供哪些服务?
定制开发 / 二次开发

基于 bluedragon/laravel-routes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 11
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-03