<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Routing\Router;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to the controller routes in your routes file.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $webNamespace = 'App\Http\Controllers\Web';
protected $apiNamespace = 'App\Http\Controllers\Api';
/**
* Define your route model bindings, pattern filters, etc.
*
* @param \Illuminate\Routing\Router $router
*
* @return void
*/
public function boot(Router $router)
{
//
parent::boot($router);
}
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
*
* @return void
*/
public function map(Router $router)
{
/*
|--------------------------------------------------------------------------
| Web Router
|--------------------------------------------------------------------------
*/
$router->group(['namespace' => $this->webNamespace], function ($router) {
require app_path('Http/routes.web.php');
});
/*
|--------------------------------------------------------------------------
| Api Router
|--------------------------------------------------------------------------
*/
$router->group(['namespace' => $this->apiNamespace], function ($router) {
require app_path('Http/routes.api.php');
});
}
}
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* This namespace is applied to your Custom controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $custom_namespace = 'App\Http\Controllers\Custom';
/**
* The path to the "home" route for your application.
*
* @var string
*/
public const HOME = '/home';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
// map new custom routes
$this->mapCustomRoutes();
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
/**
* Define the "custom" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapCustomRoutes()
{
Route::middleware('web')
->namespace($this->custom_namespace) //or change its custom_namespace to namespace if you don't need change directory of your controllers
->group(base_path('routes/custom.php')); // change custom.php to your custom routers file name
}
}
public function map()
{
/** Insert this Method Name **/
$this->methodicalness();
}
protected function methodicalness()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/yourRouteName.php'));
}
9条答案
按热度按时间pw136qt21#
在Laravel的Route上的group()方法,可以接受文件名,所以我们可以这样做:
o7jaxewo2#
1.创建2个布线文件
routes.web.php
和routes.api.php
。1.编辑
RouteServiceProvider.php
文件,使其与以下示例类似:注意:您可以添加任意数量的工艺文件...
slmsl1lt3#
您可以在任何地方创建所需数量的路由文件,然后只需在主路由文件smth中使用它们,如:
在此,您将以与main中相同的方式定义路由
yh2wf1be4#
您可以使用
Request::is()
,这样您的主routes.php
文件将如下所示:你可以阅读更多的here。
pjngdqdw5#
以防有人还在追杀
https://ctf0.wordpress.com/2016/10/01/split-routes-into-categories-in-laravel/
1-打开routes/web/api.php并添加
2-现在,在同一级别创建一个新文件夹,并将其命名为“Routes”
3-创建文件根据每个路线ex.用户,后,stuff等...并添加您的路线逻辑正常
wribegjk6#
您可以在服务提供程序中加载自定义路由文件。AsgardCMS也是这样做的,请参见加载后端路由的Core RoutingServiceProvider中的此方法:
https://github.com/AsgardCms/Core/blob/master/Providers/RoutingServiceProvider.php#L77
Laravel文档在软件包开发部分提供了一个简单的示例:
http://laravel.com/docs/5.1/packages#routing
bvk5enib7#
拉拉威尔^6
在评论中解释
rqenqsqc8#
如果你只想创建一个自定义的路由文件,你可以很容易地添加你的自定义路由文件,并使用Web中间件名称注册。
编辑应用程序\提供程序\路由服务提供程序. php
注意:这种方法也工作,即使文件是在文件夹内只是跟踪你的路线文件所在的位置。
快乐编码。
q9yhzks09#
我的简单快速的解决方案,多个路线文件。它的工作!
在web.php文件中,添加: