Jump Gate

Class Based Routing


Introduction

Class based routing is an idea to use proper PHP classes for route groups. Each class used in this way behaves like a route group. This is an alternative to using laravel’s default route files. You are always welcome to continue using those though.

RouteServiceProvider

You will first need to make sure your RouteServiceProvider can understand these classes. In the jumpgate/jumpgate package this is done for you. To set it up manually, you would need something like the RouteServiceProvider from the JumpGate package.

This class does a lot, so lets break it down.

Usage

So how does this provider help us? Let’s first look at an example route class.

<?php

namespace App\Http\Routes;

use JumpGate\Core\Contracts\Routes;
use JumpGate\Core\Http\Routes\BaseRoute;
use Illuminate\Routing\Router;

class Home extends BaseRoute implements Routes
{
    public $namespace = 'App\Http\Controllers';

    public $middleware = ['web'];

    public function routes(Router $router)
    {
        $router->get('/')
               ->name('home')
               ->uses('HomeController@index')
               ->middleware('active:home');
        
        $router->get('dashboard')
               ->name('dashboard')
               ->uses('HomeController@dashboard')
               ->middleware('active:home');
    }
}

This is a very basic example of a route class. It must implement Routes and a lot of helpers are provided for you in JumpGate\Core\Http\Routes\BaseRoute. Simply extend it if you want to.

There are a lot of properties to help define your route.

public $prefix = 'dashboard';

Lastly is the routes() method. This is where you define all the routes this group will use. These can look like normal routes using the facade or you can use the new laravel 5.3+ fluent style methods.

{warning} Once you have added this class it should begin working on the next reload. If not, look in config/routes.php and make sure the location of your route file is defined.