nonetallt/jsroute

将Laravel路由发布到JavaScript

1.2.0 2019-11-05 06:49 UTC

This package is auto-updated.

Last update: 2024-09-05 20:00:26 UTC


README

此包已弃用。请使用 laravel-publish-routes 包与 route-repository 代替。

laravel-js-routes-publish

此包允许您运行 jsroute:publish artisan 命令以生成可用于JavaScript前后端路由的路由文件。适用于与 laravel-js-routes 一起使用。

已测试与Laravel 5.5+

安装

composer require nonetallt/jsroute --dev

使用方法

php artisan jsroute:publish

配置

运行 vendor:publish 命令允许您使用 config/jsroute.php 中的 conf 文件配置包。

php artisan vendor:publish --provider="Nonetallt\Jsroute\JsrouteServiceProvider"

config/jsroute.php 中选项使用的更详细文档

<?php

return 
[
    // Determine where the output will be written
    'path' => resource_path('assets/js/routes.js'),

    // Define the groups you don't wish to publish
    'exclude_middleware' => ['api'],

    // Define the routes you don't wish to publish by name
    'exclude_by_name' => [],

    /* 
     * Sort options: priority, asc, desc 
     * priority: in order that the routes are written and will be checked 
     * uri: alphabetically by route uri
     * verb: alphabetically by http verb
     * name: alphabetically by route name
     */
    'sort_by' => 'priority',

    /*
     * Declare the sort order
     * asc: ascending (for example: start from a -> b -> etc)
     * desc: descending
     */
    'sort_order' => 'asc',

    /*
     * Determines what object the routes will be generated for.
     * It is recommended to assign the object to the window.
     * Example: window.Route = new LaravelJsRoutes();
     * In this example, the js_reference should be 'Route'.
     */
    'js_reference' => 'Route',
];

使用gulp自动发布

您可以使用 gulp 或您选择的另一个任务运行器来自动运行发布命令,每当路由文件有更改时。

安装gulp

npm install gulp --save-dev

gulpfile.js

var gulp = require('gulp');
var exec = require('child_process').exec;

gulp.task('publish-routes', function (cb) {
    exec('php artisan jsroute:publish', function(err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        cb(err);
    });
})

gulp.task('watch-routes', function() {
    gulp.watch([ 'routes/*.php'  ], ['publish-routes']);
})

运行命令

gulp watch-routes