18
NovLaravel has some pretty sweet helper functions for generating URLs/links and its auto-json-magic makes it building APIs super easy. When many of us use laravel for building single-page js apps, then routing can quickly become a bit of a pain. Wouldn’t it be amazing if we could access our Laravel routes from JavaScript?
Laroute package comes with the solution to this problem. This package enables us to port our routes over to JavaScript and gives us a bunch of very familiar helper functions to use.
You can install this package via the composer.
composer require te7a-houdini/laroute
For the configuration, you’ve to copy the package’s config files. Just run this command.
php artisan vendor:publish --provider='Te7aHoudini\Laroute\LarouteServiceProvider'
After running this command, A config file will be generated in this location.
app/config/packages/Te7aHoudini/laroute/config.php
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 public/js/laroute.js
file to include in your page, or build.
<script src="/js/laroute.js"></script>
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' });
Ignore/Filter Routes
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');
});
For more details, You can visit its documentation & source code on Github.
Published at : 18-11-2021
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project