Package Data | |
---|---|
Maintainer Username: | bmatovu |
Maintainer Contact: | mtvbrianking@gmail.com (Brian Matovu) |
Package Create Date: | 2020-06-03 |
Package Last Update: | 2023-05-01 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-15 15:20:31 |
Package Statistics | |
---|---|
Total Downloads: | 65 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 8 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 1 |
This minimalistic package will help you access exisiting PHP routes via JavaScript.
Install via Composer package manager:
composer require bmatovu/laravel-js-routes
Set application URL in the environment file; .env
.
APP_URL="http://localhost:8000"
Add application URL to base layout head meta; usually in resources/views/layouts/app.blade.php
<meta name="app-url" content="{{ config('app.url') }}">
php artisan js-routes:generate
Routes will be written to a json file: resources/js/routes.json
Publish JavaScript router to resources/js/router.js
php artisan vendor:publish --provider="Bmatovu\JsRoutes\JsRoutesServiceProvider" --tag="resources"
Load JavaScript router; usually from resources/js/bootstrap.js
var router = require('./router.js');
window.route = router.route;
npm run dev
In you routes file; web.php
make sure to use named routes.
$int = '^\d+$';
Route::pattern('user', $int);
Route::group(['prefix' => 'users', 'as' => 'users.'], function () {
Route::get('/', 'UserController@index')->name('index');
// ...
Route::delete('/{user}', 'UserController@destroy')->name('destroy');
});
In JavaScript; just get the route by name.
$.ajax({
type: 'GET',
url: route('users.index') // http://localhost:8000/users
});
// Or
$.ajax({
type: 'DELETE',
url: route('users.destroy', user.id) // http://localhost:8000/users/1
});