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 routes on the same level as the src directory – for more information refer to "Anatomy of a custom module". Depending on the type of the route, we need to create a .php file in the routes directory. 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   

The contents of the file become:


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



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 AdminModule facade 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:

  ->title('CSV Import & Export')
  ->summary('Used to import and export a variety of platform data.')

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

Articles in this section

Was this article helpful?
0 out of 0 found this helpful