How to add a custom route file in Laravel

Ntim Yeboah
1 min readJan 21, 2018

--

In Laravel, the routes/web.php and the routes/api.php are the files that you define routes for web and api respectively. In some cases, there arises the need for additional route file. For instance when you want to separate routes for each version of your api.

To add the custom route file, lets create a new file called api2.php in the routes directory. You need to register the new route file so that the routes defined in this file will be loaded by the framework. We will do this in the RouteServiceProvider file.

In the RouteServiceProvider class, define a method called mapApi2routes. In this method you can define the middleware, the namespace of the controllers that will handle requests for the routes, the route prefix, alias and the path to the route file. In this case from routes/api2.php. To get the routes registered call mapApi2routes method in the map method.

That’s it, you now have a new route file for your application.

--

--

Ntim Yeboah

Software Developer | Loves @laravelphp | Certified Laravel Developer 👨‍💻