PHPackages                             antoninmasek/laravel-route-directory-macro - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. antoninmasek/laravel-route-directory-macro

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

antoninmasek/laravel-route-directory-macro
==========================================

Adds Route::loadFromDirectory macro to Route facade, which will load all files with routes inside a directory.

0.2.0(7mo ago)3639MITPHPPHP ^8.2CI passing

Since May 18Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/antoninmasek/laravel-route-directory-macro)[ Packagist](https://packagist.org/packages/antoninmasek/laravel-route-directory-macro)[ Docs](https://github.com/antoninmasek/laravel-route-directory-macro)[ RSS](/packages/antoninmasek-laravel-route-directory-macro/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (8)Versions (6)Used By (0)

Easily load all defined routes from a directory with multiple route files.
==========================================================================

[](#easily-load-all-defined-routes-from-a-directory-with-multiple-route-files)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7df7516593f10d8364392197463a31dba8b8959284ab3060841f201471d7e78a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e746f6e696e6d6173656b2f6c61726176656c2d726f7574652d6469726563746f72792d6d6163726f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/antoninmasek/laravel-route-directory-macro)[![Total Downloads](https://camo.githubusercontent.com/77ba2828bc07a3d7902cdd22a362c53bb935de09dc85ea55cee01a3cd3116ad7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e746f6e696e6d6173656b2f6c61726176656c2d726f7574652d6469726563746f72792d6d6163726f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/antoninmasek/laravel-route-directory-macro)

This package registers `loadFromDirectory` macro on the `Route` facade. Using this macro you can easily load all defined routes from multiple files within a directory.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require antoninmasek/laravel-route-directory-macro
```

Motivation behind the macro
---------------------------

[](#motivation-behind-the-macro)

While it is perfectly all right to have all routes inside a single file, I found myself reaching for multiple files quite often. For some reason I just prefer the fact, that I have a single file per entity or domain. And to make the route registration process easier for myself I wrote a simple piece of code which I was copying from project to project. So I decided to package it up, so I don't have to do it anymore.

Basic usage
-----------

[](#basic-usage)

Consider the following routes directory structure:

```
routes/
│
├── app/
│   ├── users.php
│   ├── tasks.php
│   └── media.php
│
└── public/
    └── auth.php

```

Now, traditionally we would have to require each file from our `web.php` or any other entry point for our routes. The other option is to register each file in the `bootstrap/app.php` (or `RouteServiceProvider` for Laravel 10 and below).

Using this package we can just do the following:

```
Route::loadFromDirectory(
    path: 'routes/app',
    middleware: ['web', 'auth'],
);

Route::loadFromDirectory(
    path: 'routes/public',
    middleware: ['web'],
);
```

This will register routes from all files inside these two directories. While the first call registers routes from `routes/app` directory and assigns `web` and `auth` auth middleware, the second call registers routes from `routes/public` directory and only assigns `web` middleware.

### Laravel 11.x or later

[](#laravel-11x-or-later)

Use it inside `bootstrap/app.php`

```
return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        then: function () {
            Route::loadFromDirectory(
                'routes/app',
                ['web', 'auth'],
            );
        },
    )
```

### Laravel 10.x and below

[](#laravel-10x-and-below)

Use it inside `RouteServiceProvider.php`

```
public function boot(): void
{
    $this->configureRateLimiting();

    $this->routes(function () {
        Route::loadFromDirectory(
            'routes/app',
            ['web', 'auth'],
        );
    });
}
```

Parameters
----------

[](#parameters)

### Path

[](#path)

The only required parameter is the path to the directory with your route files. The macro uses `base_path` helper in the background, the path is relative to the base path of your project.

If you wish to use an absolute path, you have to have `/` at the start of your path string. Then the `base_path` helper won't be used.

### Middleware

[](#middleware)

This is an array, where you can specify which middleware should be applied to routes in the specified directory.

### Prefix

[](#prefix)

If you specify a prefix, then all routes in the directory will be prefixed. Let's look at the following example:

```
Route::loadFromDirectory(
    path: 'routes/admin',
    middleware: ['web', 'auth', 'admin'],
    prefix: 'admin',
);
```

Then all routes would have `admin/` prefix

### Name

[](#name)

By default, when you specify a prefix the name will automatically become the prefix with `.` at the end. So for prefix `admin` the name would be `admin.`.

If you wish to use your name, just pass it as the fourth argument. Or if you wish to just use prefix without any name set the name argument to `false`.

Hidden files
------------

[](#hidden-files)

By default, the macro excludes files starting with `.` to prevent hidden files from being loaded. You can also use this to your advantage in a scenario, where you have routes file, which you don't want to load yet. You can just prefix it with a dot and that's it. For example `routes/.secret.php`.

### Loading hidden files in specific environments

[](#loading-hidden-files-in-specific-environments)

To take even greater advantage of hidden files, you can specify environments in which you would like them to be loaded. This is very useful, when you want to work on new routes in your `local` environment, but not yet in production.

To do this you should publish the config file and specify the desired environments in the `register_hidden_routes_in_environments` config array.

```
return [
    /*
     * List of environments in which hidden routes (route files with `.` prefix) should be registered.
     *
     * This is useful for working on new routes in certain environments (usually local), while
     * ensuring they are not registered in any other environment (usually production).
     */
    'register_hidden_routes_in_environments' => [],
];
```

Credits
-------

[](#credits)

- [Antonin Masek](https://github.com/antoninmasek)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance62

Regular maintenance activity

Popularity22

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~121 days

Total

5

Last Release

239d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8faef5fac979c7f7fa3622af216db9734384499fa3890e62e9f6e97b421b204f?d=identicon)[antoninmasek](/maintainers/antoninmasek)

---

Top Contributors

[![antoninmasek](https://avatars.githubusercontent.com/u/31475951?v=4)](https://github.com/antoninmasek "antoninmasek (21 commits)")

---

Tags

laravelAntonin Maseklaravel-route-directory-macro

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/antoninmasek-laravel-route-directory-macro/health.svg)

```
[![Health](https://phpackages.com/badges/antoninmasek-laravel-route-directory-macro/health.svg)](https://phpackages.com/packages/antoninmasek-laravel-route-directory-macro)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[marcelweidum/filament-expiration-notice

Customize the livewire expiration notice

9169.0k4](/packages/marcelweidum-filament-expiration-notice)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
