How to define and orgnise routes in multiple files

Is there any official provision to define the routes in multiple files to organise our routes in a much better way? the default config/routes.php getting long and long after adding more and more routes. I wanted to define current routes in different files for different different modules.

Please guide and provide the way or configuration to achieve the same.
I am using CakePHP version 4.4

Usually its a good idea to split up your app into multiple private plugins which therefore have their own routes.php and/or routes method inside the Plugin class.

See Plugins - 4.x

“Private Plugins” in that sense, that they live inside your plugins folder instead of being managed by composer and live in the vendor folder.


Otherwise nothing is stopping your from doing a plain old simple php require like so:

// config/routes.php
<?php

use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;

/**
 * @var \Cake\Routing\RouteBuilder $routes
 */
$routes->setRouteClass(DashedRoute::class);

$routes->scope('/', function (RouteBuilder $builder) {
    // Your main routes here
});

require 'other_routes.php';
<?php
/**
 * @var \Cake\Routing\RouteBuilder $routes
 */

use Cake\Routing\RouteBuilder;

$routes->scope('/', function (RouteBuilder $builder) {
    // Your other routes
});