rinvex / tmp-lord-laroute
从 JavaScript 访问 Laravel URL/路由辅助函数。
Requires
- php: ^8.1.0
- illuminate/config: ^10.0.0 || ^11.0.0
- illuminate/console: ^10.0.0 || ^11.0.0
- illuminate/filesystem: ^10.0.0 || ^11.0.0
- illuminate/routing: ^10.0.0 || ^11.0.0
- illuminate/support: ^9.0.0 || ^10.0.0
Requires (Dev)
- mockery/mockery: dev-master
- phpunit/phpunit: ^9.5.0
README
将包名临时改为 "rinvex/tmp-lord-laroute" 以在 Packagist 上发布。这是一个临时的分支包,直到作者发布新版本,完全支持 Laravel v8+
Laroute
Laravel 提供了一些非常棒的 辅助函数 用于生成 URL/链接,其自动 JSON 魔法使其构建 API 变得非常容易。它是构建单页 JavaScript 应用的首选,但路由可能会很快变得有些痛苦。
如果我们可以从 JavaScript 中访问我们的 Laravel 路由,那岂不是太棒了?
此包允许我们将路由迁移到 JavaScript,并为我们提供了一堆 非常熟悉 的辅助函数来使用。
安装
以常规方式安装 composer。
composer.json
{ "require" : { "lord/laroute" : "2.*" } }
注意 Laravel 4.x 用户,请查看 版本 1.3.2
app/config/app.php
... 'providers' => array( ... Lord\Laroute\LarouteServiceProvider::class, ], ...
配置(可选)
复制包的配置文件。
php artisan vendor:publish --provider='Lord\Laroute\LarouteServiceProvider'
app/config/packages/lord/laroute/config.php
return [ /* * The destination path for the javascript file. */ 'path' => 'public/js', /* * The destination filename for the javascript file. */ 'filename' => 'laroute', /* * The namespace for the helper functions. By default this will bind them to * `window.laroute`. */ 'namespace' => 'laroute', /* * Generate absolute URLs * * Set the Application URL in config/app.php */ 'absolute' => false, /* * The Filter Methode * * 'all' => All routes except "'laroute' => false" * 'only' => Only "'laroute' => true" routes * 'force' => All routes, ignored "laroute" route parameter */ 'filter' => 'all', /* * Action Namespace * * Set here your controller namespace (see RouteServiceProvider -> $namespace) for cleaner action calls * e.g. 'App\Http\Controllers' */ 'action_namespace' => '', /* * The path to the template `laroute.js` file. This is the file that contains * the ported helper Laravel url/route functions and the route data to go * with them. */ 'template' => 'vendor/lord/laroute/src/templates/laroute.js', /* * Appends a prefix to URLs. By default the prefix is an empty string. * */ 'prefix' => '', ];
生成 laroute.js
要访问路由,我们需要将它们“迁移”到一个 JavaScript 文件中
php artisan laroute:generate
默认配置下,这将创建一个 public/js/laroute.js
文件,用于在页面或构建中使用。
<script src="/js/laroute.js"></script>
注意:如果您更改了路由,则需要执行 laroute:generate
。
JavaScript 文档
默认情况下,所有函数都在 laroute
命名空间下。此文档将遵循此约定。
action
为给定的控制器动作生成 URL。
/** * laroute.action(action, [parameters = {}]) * * action : The action to route to. * parameters : Optional. key:value object literal of route parameters. */ laroute.action('HomeController@getIndex');
route
为给定的命名路由生成 URL。
/** * laroute.route(name, [parameters = {}]) * * name : The name of the route to route to. * parameters : Optional. key:value object literal of route parameters. */ laroute.route('Hello.{planet}', { planet : 'world' });
url
为给定路径生成一个完全合格的 URL。
/** * laroute.url(name, [parameters = []]) * * name : The name of the route to route to. * parameters : Optional. value array of route parameters. */ laroute.url('foo/bar', ['aaa', 'bbb']); // -> /foo/bar/aaa/bbb
link_to
为给定 URL 生成一个 HTML 链接。
/** * laroute.link_to(url, [title = url, attributes = {}]]) * * url : A relative url. * title : Optional. The anchor text to display * attributes : Optional. key:value object literal of additional html attributes. */ laroute.link_to('foo/bar', 'Foo Bar', { style : "color:#bada55;" });
link_to_route
为给定路由生成一个 HTML 链接。
/** * laroute.link_to_route(name, [title = url, parameters = {}], attributes = {}]]]) * * name : The name of the route to route to. * title : Optional. The anchor text to display * parameters : Optional. key:value object literal of route parameters. * attributes : Optional. key:value object literal of additional html attributes. */ laroute.link_to_route('home', 'Home');
link_to_action
为给定动作生成一个 HTML 链接。
/** * laroute.link_to_action(action, [title = url, parameters = {}], attributes = {}]]]) * * action : The action to route to. * title : Optional. The anchor text to display * parameters : Optional. key:value object literal of route parameters. * attributes : Optional. key:value object literal of additional html attributes. */ laroute.link_to_action('HelloController@planet', undefined, { planet : 'world' });
PHP 文档
忽略/过滤路由
默认情况下,所有路由在执行 php artisan laroute:generate
后都可用于 laroute。然而,有时可能希望 laroute 忽略某些路由。您可以通过传递一个 laroute
路由选项来实现这一点。
Route::get('/ignore-me', [ 'laroute' => false, 'as' => 'ignoreme', 'uses' => 'IgnoreController@me' ]); Route::group(['laroute' => false], function () { Route::get('/groups-are-super-useful', 'GroupsController@index'); });