Laravel routes from Javascript

Overview

Laravel Javascript Routes

Travis Badge Latest Stable Version Latest Unstable Version Total Downloads License

Why?

I love the Laravel 4 routing system and I often use named routes like route('users.show', array('id' => 1)) to generate http://domain.tld/users/1. With the amazing uprising of Javascript frameworks (AngularJS, EmberJS, Backbone, etc.) it's hard to track changes on your routes between the backend and the REST calls from your Javascript. The goal of this library is to expose those named routes to your frontend so you can do: Router.route('users.show', {id: 1}) and get the same result.

Laravel 5

I haven't had the time to update this package to Laravel 5. Eventually, I'll create a new repo for that.

Requirements

  • Laravel 4.1

Installation

Begin by installing this package through Composer. Edit your project's composer.json file to require fedeisas/laravel-4-js-routes.

{
  "require": {
        "laravel/framework": "4.0.*",
        "fedeisas/laravel-4-js-routes": "1.*"
    },
    "minimum-stability" : "dev"
}

Next, update Composer from the Terminal:

$ composer update

Once this operation completes, the final step is to add the service provider. Open app/config/app.php, and add a new item to the providers array.

'Fedeisas\LaravelJsRoutes\LaravelJsRoutesServiceProvider',

Usage

By default the command will generate a routes.js file on your project root. This contains all named routes in your app. That's it! You're all set to go. Run the artisan command from the Terminal to see the new routes:javascript commands.

$ php artisan routes:javascript

Lazy Tip If you use Grunt, you could set up a watcher that runs this command whenever your routes files change.

Arguments

Name Default Description
name routes.js Output filename

Options

Name Default Description
path base_path() Where to save the generated filename. (ie. public assets folder)
filter null If you want only some routes to be available on JS, you can use a filter (like js-routable) to select only those
object Router If you want to choose your own global JS object (to avoid collision)
prefix null If you want to a path to prefix to all your routes

Javascript usage

You have to include the generated file in your views (or your assets build process).

<script src="/path/to/routes.js" type="text/javascript">

And then you have a Routes object on your global scope. You can use it as:

Router.route(route_name, params)

Example:

Router.route('users.show', {id: 1}) // returns http://dommain.tld/users/1

If you assign parameters that are not present on the URI, they will get appended as a query string:

Router.route('users.show', {id: 1, name: 'John', order: 'asc'}) // returns http://dommain.tld/users/1?name=John&order=asc

Contributing

$ composer install --dev
$ ./vendor/bin/phpunit
$ npm install -g grunt-cli
$ npm install
$ grunt travis --verbose

In addition to a full test suite, there is Travis integration.

Found a bug?

Please, let me know! Send a pull request or a patch. Questions? Ask! I will respond to all filed issues.

Inspiration

Although no code was copied, this package is greatly inspired by FOSJsRoutingBundle for Symfony.

License

This package is open-sourced software licensed under the MIT license

Comments
  • Laravel 5 compatibility

    Laravel 5 compatibility

    See #15

    I would suggest to create a new branch and put the Laravel 4 code in there. The master branch should refer to this package compatible with Laravel 5.

    The README still needs adjustments then.

    opened by dhardtke 6
  • Laravel 5 compatibility?

    Laravel 5 compatibility?

    Hey,

    thanks for your awesome Laravel 4 package. I was using it for a long time and right now I am working on my app to make it Laravel 5 compatible. Hence my question: is this package Laravel 5 compatible?

    Thanks.

    Edit: Okay, tried installing and I'm getting:

    Your requirements could not be resolved to an installable set of packages.

    opened by dhardtke 5
  • Unable to install js-routes in Laravel 4.0

    Unable to install js-routes in Laravel 4.0

    Hi, I tried to follow your instructions to add js-routes into my project, but now composer update returns me this error. Does it remind you of something ?

    Your requirements could not be resolved to an installable set of packages.
    
      Problem 1
        - Conclusion: remove laravel/framework 4.0.x-dev
        - Conclusion: don't install laravel/framework 4.0.x-dev
        - Conclusion: don't install illuminate/support 4.2.x-dev
        - Conclusion: don't install laravel/framework v4.0.10
        - illuminate/routing 4.2.x-dev requires illuminate/support 4.2.* -> satisfia
    ble by laravel/framework[4.2.x-dev], illuminate/support[4.2.x-dev].
        - Conclusion: don't install laravel/framework 4.2.x-dev
        - Conclusion: don't install laravel/framework v4.0.9
        - Conclusion: don't install laravel/framework 4.1.x-dev
        - Conclusion: don't install laravel/framework v4.0.8
        - Conclusion: don't install laravel/framework v4.1.21
        - Conclusion: don't install laravel/framework v4.0.7
        - Conclusion: don't install laravel/framework v4.1.20
        - Conclusion: don't install laravel/framework v4.0.6
        - Conclusion: don't install laravel/framework v4.1.19
        - Conclusion: don't install laravel/framework v4.0.5
        - Conclusion: don't install laravel/framework v4.1.18
        - Conclusion: don't install laravel/framework v4.0.4
        - Conclusion: don't install laravel/framework v4.1.17
        - Conclusion: don't install laravel/framework v4.0.3
        - Conclusion: don't install laravel/framework v4.1.16
        - Conclusion: don't install laravel/framework v4.0.2
        - Conclusion: don't install laravel/framework v4.1.15
        - Conclusion: don't install laravel/framework v4.0.1
        - Conclusion: don't install laravel/framework v4.1.14
        - Conclusion: don't install laravel/framework v4.0.0
        - Conclusion: don't install laravel/framework v4.1.12|install laravel/framew
    
    opened by ghost 5
  • Error sending a url in Laravel

    Error sending a url in Laravel

    Hi

    I am trying to send a parameter by a url from Javascript like this: var url=Router.route('getTeams',{id:1}); and I know that the url works, but when I try to send the parameter it send it like this: "/getTeams?id=1" and It should be "/getTeams/1". So I want you to help me because I don't know how to make it work.

    Thanks

    opened by alexanderzapata27 4
  • Class 'Fedeisas\LaravelJsRoutes\LaravelJsRoutesServiceProvider' not found

    Class 'Fedeisas\LaravelJsRoutes\LaravelJsRoutesServiceProvider' not found

    Hey

    Was trying your package, but was unable to use it properly. I'm using Laravel 4.1 with 1.3 of your package. After composer update, it says the serviceprovider is not found. The package is in the vendor folder, but not inside of the autoloader. (When I add it manually to that file, it works perfectly btw!)

    I'm using the psr-0 autoloader (just like laravel and a lot of other laravel packages), so I'm guessing the issue has to do with you using psr-4. How did you get it working with your laravel installation?

    Love the idea of the package btw! Could be very useful (though I'm not completely sure how secure it is to have your routes listed inside a file?) Maybe you could make a config file with some basic setting such as storage path (which is now the default base_path()), would be useful, so I won't have to type the entire path everytime I call the command :)

    opened by MaatwebsiteSupport 4
  • accounting of optional parameters of routes

    accounting of optional parameters of routes

    Example: //routes.php Route::get('/cart/add/{article}/{count?}', ['uses' => 'CartController@add', 'as' => 'basket_add']);

    // JS

    1. Router.route('basket_add', {article: 'article1', count: 2})
      => http://host/cart/add/article1/{count?}?count=2

    2. Router.route('basket_add', {article: 'article1'}) => http://host/cart/add/article1/{count?}

    opened by DmitryPaschenko 2
  • Remove version from composer.json

    Remove version from composer.json

    Since you are tagging this, you should remove the version from the composer.json as composer will automatically match the version to tag number. Currently I am unable to download tag 1.5 because 1.4 is hardcoded in the package.

    opened by silentworks 1
  • Website URL subfolder

    Website URL subfolder

    Noticed that this only works with base URL's (www.mysite.com) and doesn't respect sites with subfolder configurations (www.mysite.com/mylaravelsite/). Is this something you would entertain adding?

    opened by mpemberton5 0
  • The requested package fedeisas/laravel-4-js-routes could not be found in any version, there may be a typo in the package name.

    The requested package fedeisas/laravel-4-js-routes could not be found in any version, there may be a typo in the package name.

    Hello,

    thanks for your great job !

    With composer 1.0-dev when I type: composer update after add "fedeisas/laravel-4-js-routes": "1.*" in composer.json, composer return : The requested package fedeisas/laravel-4-js-routes could not be found in any version, there may be a typo in the package name. But when I type : composer require fedeisas/laravel-js-routes, composer return Using version ^1.6 for fedeisas/laravel-js-routes and the package is correctly add in project.

    It may not really be an issue, but I want to report this to you.

    opened by Nolwennig 0
  • Missing required variables are not detected

    Missing required variables are not detected

    When a required path variable is not provided the router leaves the token in the generated url. Say I have a route with the uri /page/{id}/edit. If I pass an empty params argument the generated uri will be /page/{id}/edit. I think it is more helpful to detect the missing parameter and raise an error.

    I see two options: 1/ Compile the routes server side and add the variables in the exported content 2/ Write JS code that compiles the route (maybe not a complete compilation as it is done server side, but extract enough information for this use case).

    opened by seydu 0
Owner
Fede Isas
Fede Isas
Pretty routes for Laravel

Pretty Routes for Laravel Visualise your routes in pretty format. Installation composer require garygreen/pretty-routes If your using autodiscovery in

Gary Green 644 Dec 15, 2022
View your Laravel routes on the browser.

View your Laravel routes on the browser. This package adds a route to your Laravel application. Once you've installed this package, enter /route-list

Patompong Savaengsuk 23 Oct 28, 2022
Declare routes inside Laravel Livewire components.

Convoy This package allows you to declare routes inside of your full page Laravel Livewire components. All you have to do is create a route method ins

null 17 Jul 27, 2022
A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, ACCESSIBLE (WAI-ARIA) REPLACEMENT FOR JAVASCRIPT'S POPUP BOXES FOR LARAVEL

A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, ACCESSIBLE (WAI-ARIA) REPLACEMENT FOR JAVASCRIPT'S POPUP BOXES FOR LARAVEL Install To get started with SweetAle

Rashid Ali 939 Jan 8, 2023
Use ESM with importmap to manage modern JavaScript in Laravel without transpiling or bundling

Introduction Use ESM with importmap to manage modern JavaScript in Laravel without transpiling or bundling. Inspiration This package was inspired by t

Tony Messias 91 Dec 30, 2022
A Laravel clone of the Javascript Flatpickr (Date picker) library

A Laravel clone of the Javascript Flatpickr (Date picker) library Using this package you can add a beautiful date or datetime picker into your project

Laratips 49 Dec 28, 2022
Video Chat application built using Metered Video SDK, with PHP Laravel Backend and JavaScript Front-End

Group Video Chat App with PHP Laravel and JavaScript Powered by Metered Video SDK Overview This application is a highly scalable group video calling a

null 2 Aug 18, 2022
Lavacharts is a graphing / charting library for PHP 5.4+ that wraps Google's Javascript Chart API.

Lavacharts 3.1.12 Lavacharts is a graphing / chart library for PHP5.4+ that wraps the Google Chart API. Stable: Dev: Developer Note Please don't be di

Kevin Hill 616 Dec 17, 2022
Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Jameson Lopp 28 Dec 19, 2022
Simplifies writing DocBlock comments in Javascript, PHP, CoffeeScript, Actionscript, C & C++

DocBlockr DocBlockr is a package for Sublime Text 2 & 3 which makes writing documentation a breeze. DocBlockr supports JavaScript (including ES6), PHP

Nick Fisher 3.1k Nov 25, 2022
Register for multiple Livestorm sessions from an external form. Practical use of Livestorm API with PHP/Javascript.

Livestorm Multi Session Registration Register for multiple Livestorm sessions from an external form. Practical use of Livestorm API with PHP/Javascrip

Nathan CHEVALIER 0 Dec 24, 2021
Loja virtual fictícia para compra de produtos e estilização dos mesmos. Desenvolvido com as tecnologias: HTML, CSS, PHP, CODEIGNITER, JavaScript, Bootstrap e Mysql.

StampGeek Loja virtual fictícia para compra de produtos e estilização dos mesmos. Desenvolvido com as tecnologias: HTML, CSS, PHP, CODEIGNITER, JavaSc

Pablo Silva 1 Jan 13, 2022
The Most Popular JavaScript Calendar as a Filament Widget 💛

The Most Popular JavaScript Calendar as a Filament Widget ?? Features Accepts all configurations from FullCalendar Event click and drop events Upcomin

Guilherme Saade 62 Dec 31, 2022
PcTools is a proyect developed using {JavaScript,HTML5,CSS} for frontend and {PHP => Mysql} for backend.

PcTools-Proyect PcTools is a proyect developed using {JavaScript,HTML5,CSS} for frontend and {PHP => Mysql} for backend. Future Improvements # Replace

Ihab Fallahy 1 Feb 5, 2022
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Jan 2, 2023
⚡ Laravel Charts — Build charts using laravel. The laravel adapter for Chartisan.

What is laravel charts? Charts is a Laravel library used to create Charts using Chartisan. Chartisan does already have a PHP adapter. However, this li

Erik C. Forés 31 Dec 18, 2022
Laravel Kickstart is a Laravel starter configuration that helps you build Laravel websites faster.

Laravel Kickstart What is Laravel Kickstart? Laravel Kickstart is a Laravel starter configuration that helps you build Laravel websites faster. It com

Sam Rapaport 46 Oct 1, 2022
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

null 9 Dec 14, 2022
Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

Laravel Segment Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application. Installation You can install the pac

Octohook 13 May 16, 2022