Skip to main content

How do I setup routes for my custom module?

There are a few necessary steps to create and properly configure module routing.

Creating routes

The first step to creating module routes is to create a directory named routeson the same level as the srcdirectory – for more information refer to "Anatomy of a custom module.phpfile in the routesdirectory. If my module only requires admin routes, I’ll create a file called admin.php in the routes directory:

└─ module   
    └─ database   
        └─ migrations   
    └─ public   
    └─ resources   
        └─ css   
        └─ js   
       └─ views   
           └─ admin   
          └─ store   
    └─ routes   
        └─ admin.php
    └─ src   
        └─ Http   
       └─ Controllers   
       └─ Responses   
           └─ Steps   
       └─ Requests   
       └─ Models   
    ServiceProvider.php   
.gitignore   
composer.json   
README.md   

The contents of the file become:

<?php

use Illuminate\Support\Facades\Route;

The file can then be populated with the necessary routes the module needs. For more information on routes see the laravel documentaiton on routing

Loading routes in the Service Providers

The module needs to be made aware of all the routes that are available for it, and this is how it can be done:

Single routes file

$this->loadRoutesFrom(__DIR__.'/../routes/routes.php');

Store routes

Router::addStoreRoutes(__DIR__ . '/../routes/web.php');

Admin routes

Router::addAdminRoutes(__DIR__ . '/../routes/admin.php');

Using AdminModule facade

Routes can also be added through the AdminModulefacade which allows the chaining for both **route()**and routes(). This should only be used with modules that have an interface/access point in the admin modules section. It can be done like so:

AdminModule::create('csv')
  ->title('CSV Import & Export')
  ->summary('Used to import and export a variety of platform data.')
  ->routes(__DIR__.'/../routes/admin.php')
  ->route('admin.csv.index');

Where the **route()**accesses a declared route that returns a view or routes(), with the same principle as the above examples.