# Routes

**Package:** Laravel Modules | **Version:** 3 | **URL:** https://mozex.dev/docs/laravel-modules/v3/features/routes

---

## Overview

Auto-discovers route files in modules and loads them with sensible defaults. Routes are grouped by filename (`web.php` → `web` group, `api.php` → `api` group). Special filenames handle broadcasting channels (`channels.php`) and console routes (`console.php`). Groups and registrars are customizable via the `Modules` facade.

## Default configuration

```php
'routes' => [
    'active' => true,
    'patterns' => [
        '*/Routes/*.php',
    ],
    'commands_filenames' => [
        'console',
    ],
    'channels_filenames' => [
        'channels',
    ],
],
```

## Directory layout

```
Modules/Blog/
└── Routes/
    ├── web.php         // grouped with 'web' middleware
    ├── api.php         // grouped with 'api' prefix + middleware
    ├── admin.php       // custom group (if configured)
    ├── channels.php    // broadcast channel definitions
    └── console.php     // console routes (Laravel 10+)
```

## Groups and registration

Two groups are pre-defined:

```php
Modules::routeGroup('api', prefix: 'api', middleware: ['api']);
Modules::routeGroup('web', middleware: ['web']);
```

- The filename is the group key: `web.php` → `web`, `api.php` → `api`.
- Files with unrecognized names get no middleware or prefix.

### Custom groups

```php
// In a service provider's register() method
Modules::routeGroup('admin',
    prefix: 'admin',
    middleware: ['web', 'auth'],
    as: 'admin::'
);
```

Attribute values can be closures (evaluated at registration time):

```php
Modules::routeGroup('api',
    prefix: fn () => config('app.api_prefix', 'api'),
    middleware: ['api', 'throttle:api']
);
```

### Custom registrars

Override how a group registers its routes:

```php
Modules::registerRoutesUsing('localized', function (array $attributes, $routes) {
    Route::localized(function () use ($attributes, $routes): void {
        Route::group($attributes, $routes);
    });
});

Modules::routeGroup('localized', middleware: ['web'], as: 'localized::');
```

The registrar key matches the route filename (`localized.php`).

> Call `Modules::routeGroup()` and `Modules::registerRoutesUsing()` from a service provider's `register()` method so groups are defined before route discovery.

## Broadcasting channels

If any `channels.php` files are discovered, `Broadcast::routes()` is called once after the app boots and all channel files are required:

```php
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});
```

## Console routes

`console.php` files are added to the console kernel. Use the `Schedule` facade inside `console.php` to register scheduled tasks per module.

## Configuration

- Set `'routes.active' => false` to disable module route loading.
- Edit `'routes.patterns'`, `'routes.commands_filenames'`, and `'routes.channels_filenames'` to match your conventions.

## Troubleshooting

- **Wrong group**: the filename is the group key. Define attributes via `Modules::routeGroup('admin', ...)`.
- **Changes not visible**: clear route cache (`php artisan route:clear`).
- **Channels not loading**: place definitions in `Modules/*/Routes/channels.php`.

---

## Table of Contents

- [Introduction](https://mozex.dev/docs/laravel-modules/v3)
- [Support Us](https://mozex.dev/docs/laravel-modules/v3/support-us)
- [Requirements](https://mozex.dev/docs/laravel-modules/v3/requirements)
- [Changelog](https://mozex.dev/docs/laravel-modules/v3/changelog)
- [Contributing](https://mozex.dev/docs/laravel-modules/v3/contributing)
- [Questions & Issues](https://mozex.dev/docs/laravel-modules/v3/questions-and-issues)
- [About Mozex](https://mozex.dev/docs/laravel-modules/v3/about)

### Features

- [Blade Components](https://mozex.dev/docs/laravel-modules/v3/features/blade-components)
- [Views](https://mozex.dev/docs/laravel-modules/v3/features/views)
- [Routes](https://mozex.dev/docs/laravel-modules/v3/features/routes)
- [Configs](https://mozex.dev/docs/laravel-modules/v3/features/configs)
- [Migrations](https://mozex.dev/docs/laravel-modules/v3/features/migrations)
- [Seeders](https://mozex.dev/docs/laravel-modules/v3/features/seeders)
- [Commands](https://mozex.dev/docs/laravel-modules/v3/features/commands)
- [Helpers](https://mozex.dev/docs/laravel-modules/v3/features/helpers)
- [Models & Factories](https://mozex.dev/docs/laravel-modules/v3/features/models-factories)
- [Policies](https://mozex.dev/docs/laravel-modules/v3/features/policies)
- [Events & Listeners](https://mozex.dev/docs/laravel-modules/v3/features/events-listeners)
- [Service Providers](https://mozex.dev/docs/laravel-modules/v3/features/service-providers)
- [Translations](https://mozex.dev/docs/laravel-modules/v3/features/translations)
- [Caching](https://mozex.dev/docs/laravel-modules/v3/features/caching)
- [Listing Modules](https://mozex.dev/docs/laravel-modules/v3/features/listing)
- [Livewire Components](https://mozex.dev/docs/laravel-modules/v3/features/livewire-components)
- [Filament](https://mozex.dev/docs/laravel-modules/v3/features/filament)
- [Nova Resources](https://mozex.dev/docs/laravel-modules/v3/features/nova-resources)

### Integrations

- [PHPStan](https://mozex.dev/docs/laravel-modules/v3/integrations/phpstan)
- [PHPUnit](https://mozex.dev/docs/laravel-modules/v3/integrations/phpunit)
- [Pest](https://mozex.dev/docs/laravel-modules/v3/integrations/pest)