jeylabs/jsroute

从 JavaScript 访问 Laravel 路由辅助函数。

v1.0.2 2015-06-02 08:45 UTC

This package is auto-updated.

Last update: 2024-08-29 04:09:04 UTC


README

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