Forked and adjusted version of aaronlord/laroute.
Install the usual composer way.
{
"require" : {
"jojo/laroute" : "2.*"
}
}
n.b Laravel 4.x users, check out version 1.3.2
...
'providers' => array(
...
Jojo\Laroute\LarouteServiceProvider::class,
],
...
Copy the packages config files.
php artisan vendor:publish --provider='Jojo\Laroute\LarouteServiceProvider'
return [
/*
* The destination path for the javascript file.
*/
'path' => 'resources/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 Method
*
* 'all' => All routes except "'laroute' => false"
* 'only' => Only "'laroute' => true" routes
* 'force' => All routes, ignored "laroute" route parameter
*/
'filter' => 'all',
/*
* Controller 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' => [
'js' => 'vendor/jojo/laroute/src/templates/laroute.js',
'json' => 'vendor/jojo/laroute/src/templates/laroute.json',
],
/*
* Appends a prefix to URLs. By default the prefix is an empty string.
*
*/
'prefix' => '',
/*
* Default generated file type (js, json)
*/
'type' => 'js',
];
To access the routes, we need to "port" them over to a JavaScript file:
php artisan laroute:generate
With the default configuration, this will create a resources/js/laroute.js
file to include in your page, or build.
Note: You'll have to laroute:generate
if you change your routes.
By default, all of the functions are under the laroute
namespace. This documentation will stick with this convention.
Generate a URL for a given controller action.
/**
* laroute.action(action, [parameters = {}])
*
* action : The action to route to.
* parameters : Optional. key:value object literal of route parameters.
*/
laroute.action('HomeController@getIndex');
Generate a URL for a given named route.
/**
* 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' });
Generate a fully qualified URL to the given path.
/**
* 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
Generate a html link to the given url.
/**
* 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;" });
Generate a html link to the given route.
/**
* 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');
Generate a html link to the given action.
/**
* 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' });
By default, all routes are available to laroute after a php artisan laroute:generate
. However, it is sometimes desirable to have laroute ignore certain routes. You can do this by passing a laroute
route option.
Route::get('/ignore-me', [
'laroute' => false,
'as' => 'ignoreme',
'uses' => 'IgnoreController@me'
]);
Route::group(['laroute' => false], function () {
Route::get('/groups-are-super-useful', 'GroupsController@index');
});