lakedawson/apirouter

Customisable router for Laravel 4.1+


Keywords
route, api, router, laravel, angularjs, angular
License
MIT

Documentation

ApiRouter

Build Status License Latest Stable Version

Customisable resource router for Laravel 4.

Copyright (c) 2014 Scott Dawson.

Documentation

Background

We primarily build large scale, multi tenant, single page applications based on AngularJS and Laravel 4. We are the type of people that want to write as little code as possible, and out of the box the Laravel router has a very specific way of creating resource routes.

Since we use Vocal in our applications, we definitely don't need two methods for saving records (store and update) we can handle it all with one method (save).

To handle the routes how we want to use them we use our own router, and now you can use it too.

Installation

The first thing you need to do is add sjdaws/apirouter as a requirement to composer.json:

{
    "require": {
        "sjdaws/apirouter": "0.2.*"
    }
}

After updating your composer.json run composer update.

Usage

After you've installed ApiRouter, we need to tell Laravel to use it. Find the providers key in app/config/app.php and register the ApiRouter:

'providers' => array(
    // ...

    'Sjdaws\ApiRouter\ApiRouterServiceProvider',
)

Note: All your routes will continue to function as normal with the exception of resource routes. You don't need to replace all your routes to use ApiRouter.

Getting Started

By default, API router provides the following methods:

Config name Method Path Function Route name
destroy DELETE /resource/{resource} destroy resource.destroy
manifest GET/HEAD /resource manifest resource.manifest
get GET/HEAD /resource/{resource} get resource.get
store POST /resource save resource.store
restore POST /resource/{resource} restore resource.restore
update PUT /resource/{resource} save resource.update
patch PATCH /resource/{resource} save resource.patch

You can easily add your own from the configuration. To add your own configuration, you can add the default configuration to your application by running config:publish command line in the root directory of your application:

$ php artisan config:publish sjdaws/apirouter

This will add the file app/config/packages/sjdaws/apirouter/config.php to your applications config directory.

Configuration

To configure a new route, just enter it into the configuration file in the following format:

'name' => array(
    'method'   => 'delete|get|patch|post|put',
    'id'       => true|false,
    'function' => 'functionName'|null,
    'suffix'   => 'suffix'|null
);

Where:

  • name is the route name, this will be added to the route as route.name. This will also be used as the function name unless a function is specified
  • method (optional) is the method used to call the route, valid options are delete, get, patch, post, and put. If method isn't specified get will be assumed.
  • id (optional) is whether or not you're passing an id to the uri. An example of this would be for fetching a single record, it will appear in the route list as resource/{resource}. If id isn't specified, false will be assumed.
  • function (optional) the function name to call from the controller. This will allow you to route multiple routes to a single function. If function is not specified, name will be used.
  • suffix (optional) is the path to put after the request e.g. resource/{resource}/suffix. This can be used if you want to make railesque routes such as resource/{resource}/edit or resource/{resource}/delete.

Note: overlapping routes will not be honoured. For example, there can only be one route with method => get and id => false. If multiple routes of the same type are specified, only the first route will be honoured.