New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

bmatovu/laravel-js-routes

Laravel Javascript routes.
71 8
Install
composer require bmatovu/laravel-js-routes
Latest Version:v2.2.1
PHP:^7.0|^8.0
Laravel:5 – 10
License:MIT
Last Updated:May 1, 2023
Links: GitHub  ·  Packagist
Maintainer: bmatovu

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

You should .gitignore the above auto-generated file.

Publish resources

Publish JavaScript router to resources/js

php artisan vendor:publish --provider="Bmatovu\JsRoutes\JsRoutesServiceProvider"

Using Webpack | Laravel Mix

Load JavaScript router; usually in resources/js/app.js

window.route = require('./router.js').route;

console.log(route('login'));

Using ViteJS

import { route } from './router.mjs';
window.route = route;

console.log(route('login'));

Compile JS routes

npm run dev

Usage

Sample Laravel (named) routes

$int = '^\d+$';

Route::pattern('post', $int);
Route::pattern('comment', $int);

Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
    Route::get('/', 'PostController@index')->name('index');
    Route::get('/{post}/comments/{comment?}', 'PostController@comments')->name('comments');
    Route::delete('/{post}', 'PostController@destroy')->name('destroy');
});

In JavaScript; just get the route by name.

axios.get(route('posts.index'));
// http://localhost:8000/posts

axios.get(route('posts.comments', {'post': post.id}));
// http://localhost:8000/posts/1/comments

axios.get(route('posts.comments', {'post': post.id, 'comment': comment.id}));
// http://localhost:8000/posts/1/comments/4

axios.get(route('posts.comments', {'post': post.id, 'comment': comment.id, 'page': 2, 'size': 10}));
// http://localhost:8000/posts/1/comments/4?page=2&size=10

axios.delete(route('posts.destroy', {'post': post.id}));
// http://localhost:8000/posts/1

axios.get(route('posts.index', {'published-at': '2020-09-23 16:42:12'}));
// http://localhost:8000/posts?published-at=2020-09-23%2016:42:12

axios.get(route('posts.index', {'with': ['author', 'comments']}));
// http://localhost:8000/posts?with=author,comments

axios.get(route('posts.index', {'with[0]': 'author', 'with[1]': 'comments'}));
// http://localhost:8000/posts?with[0]=author&with[1]=comments

Related Packages

tightenco/ziggy

Use your Laravel named routes in JavaScript.

60,755,852 4,318
lord/laroute

Access Laravels URL/Route helper functions, from JavaScript.

2,045,876 786
te7a-houdini/laroute

Access Laravels URL/Route helper functions, from JavaScript.

513,004 33
microweber-deps/ziggy

Generates a Blade directive exporting all of your named Laravel routes. Also pro...

120,256 0
surgio/laroute

Access Laravels URL/Route helper functions, from JavaScript.

50,282 0

Version History

Version Released PHP Laravel License
v2.2.1 ^7.0|^8.0 ^5.5|^6.0|^7.0|^8.0|^9.0|^10.0 MIT
v2.2.0 ^7.0|^8.0 ^5.5|^6.0|^7.0|^8.0|^9.0|^10.0 MIT
v2.1.0 >=7.0.0 ^5.5|^6.0|^7.0|^8.0 MIT
v2.0.0 >=7.0.0 ^5.5|^6.0|^7.0|^8.0 MIT
v1.2.0 >=7.0.0 ^5.5|^6.0|^7.0|^8.0 MIT