gefar /laroute
从JavaScript访问Laravels URL/路由辅助函数。
2.5.0
2024-06-05 13:56 UTC
Requires
- php: >=5.4.0
- illuminate/config: 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*
- illuminate/console: 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*
- illuminate/filesystem: 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*
- illuminate/routing: 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*
- illuminate/support: 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*
Requires (Dev)
- mockery/mockery: dev-master
- phpunit/phpunit: ~4.0
README
Laravel有一些非常棒的辅助函数用于生成URL/链接,其自动JSON魔法使得构建API变得非常简单。它是构建单页JS应用的优选方案,但路由可能很快就会变得有点麻烦。
如果我们能从JavaScript访问Laravel路由会怎么样呢?
这个包允许我们将路由移植到JavaScript中,并提供了一系列非常熟悉的辅助函数供我们使用。
安装
按照常规的composer方式进行安装。
composer.json
{ "require" : { "gefar/laroute" : "2.*" } }
注意:Laravel 4.x用户,请查看版本1.3.2
app/config/app.php
... 'providers' => array( ... Gefar\Laroute\LarouteServiceProvider::class, ], ...
配置(可选)
复制包的配置文件。
php artisan vendor:publish --provider='Gefar\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-api', /* * The namespace for the helper functions. By default this will bind them to * `window.laroute`. */ 'namespace' => 'larouteApi', /* * 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-api.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-api.js', /* * Appends a prefix to URLs. By default the prefix is an empty string. * */ 'prefix' => '', ];
生成laroute-api.js
要访问路由,我们需要将它们“移植”到一个JavaScript文件中
php artisan laroute:generate
默认配置下,这将创建一个public/js/laroute-api.js
文件,您可以在页面中包含该文件或构建。
<script src="/js/laroute-api.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'); });