mtvbrianking / laravel-js-routes by bmatovu

Laravel Javascript routes.
65
8
1
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

Laravel JS Routes.

Build Status Scrutinizer Code Quality Code Coverage StyleCI Documentation

This minimalistic package will help you access exisiting PHP routes via JavaScript.

Installation

Install via Composer package manager:

composer require bmatovu/laravel-js-routes

Setup

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') }}">

Generate routes

php artisan js-routes:generate

Routes will be written to a json file: resources/js/routes.json

Publish resources

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;

Compile JS routes

npm run dev

Usage

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
});