Ad

Is It Possible To More Than One Route File For A Single Laravel App?

- 1 answer

In my Laravel app, I have a route file in the app/HTTP directory. Can I have another route file, like I have different Controller files?

If it is possible, how can I redirect to urls to each files? Can anyone give me an example so I can understand this better?

Ad

Answer

Yep, you can!

First, make a new file (let's call it "other-routes.php") in the app/Http directory. We'll come back to this in the next step.

Step two: Go to your app/Providers/RouteServiceProvider.php file.

Go to the map method (shown below)

/**
 * Define the routes for the application.
 *
 * @param  \Illuminate\Routing\Router  $router
 * @return void
 */
public function map(Router $router)
{
    $router->group(['namespace' => $this->namespace], function ($router) {
        require app_path('Http/routes.php');
    });
}

Now, see where it requires the app/Http/routes.php file? Duplicate that single line of code, and change it to your new file (be sure to reference the old one as well, though!)

If I did it, it would look like:

/**
 * Define the routes for the application.
 *
 * @param  \Illuminate\Routing\Router  $router
 * @return void
 */
public function map(Router $router)
{
    $router->group(['namespace' => $this->namespace], function ($router) {
        require app_path('Http/routes.php');
        require app_path('Http/my-new-routes-file.php');
    });
}

Now, just add a route to your new routes file, test it to make sure it responds properly, and if it does, you're ready to go on!

I hope this helped.

Ad
source: stackoverflow.com
Ad