fighter
fighter

Reputation: 149

Is it possible to more than one route file for a single Laravel app?

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?

Upvotes: 0

Views: 158

Answers (2)

Ayo Akinyemi
Ayo Akinyemi

Reputation: 777

Here is another good example of how you can have different route classes:

https://github.com/StyleCI/StyleCI/tree/master/app/Http/Routes

and you can map it like so -

https://github.com/StyleCI/StyleCI/blob/master/app/Foundation/Providers/RouteServiceProvider.php#L80

Upvotes: 0

CoderLeo
CoderLeo

Reputation: 324

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.

Upvotes: 2

Related Questions