在我的Laravel应用程序中,app/HTTP
目录中有一个路由文件。我可以有另一个路由文件吗,就像我有不同的控制器文件一样?
如果可能的话,我如何重定向到每个文件的url?有人能给我举个例子让我更好地理解这一点吗?
是的,你可以!
首先,在app/Http目录中创建一个新文件(我们称之为"otherroutes.php")。我们将在下一步回到这一点。
第二步:转到您的app/Providers/RouteServiceProvider.php
文件。
转到map
方法(如下所示)
/**
* 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/Http/routes.php
文件的哪里?复制这一行代码,并将其更改为您的新文件(不过,请确保也引用旧文件!)
如果我这样做了,它会看起来像:
/**
* 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');
});
}
现在,只需在新的路由文件中添加一条路由,对其进行测试以确保其响应正确,如果响应正确,您就可以继续了!
我希望这能有所帮助。
下面是另一个关于如何拥有不同路由类的好例子:
https://github.com/StyleCI/StyleCI/tree/master/app/Http/Routes
你可以这样映射它-
https://github.com/StyleCI/StyleCI/blob/master/app/Foundation/Providers/RouteServiceProvider.php#L80