php 多个路由文件而不是 laravel 5 中的一个主路由文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34182806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiple route file instead of one main route file in laravel 5
提问by loghman
I am a novice in web developing with Laravel 5. I installed asGgardCMS and After seeing asgardCms codes, I found that there is nothing codes in app/Http/route.php file and required codes for routing be placed in Modules codes. For example required code for routing menu manager module be placed in Modules/Media/apiRoutes.php and Modules/Media/backendRoutes.php files. May help me and tell me how I can manage my routes like that?
我是用 Laravel 5 进行 web 开发的新手。我安装了 asGgardCMS,在看到 asgardCms 代码后,我发现 app/Http/route.php 文件中没有任何代码,路由所需的代码放在 Modules 代码中。例如,路由菜单管理器模块所需的代码放置在 Modules/Media/apiRoutes.php 和 Modules/Media/backendRoutes.php 文件中。可以帮助我并告诉我如何管理我的路线吗?
采纳答案by tommy
You can load custom route files within a Service Provider. AsgardCMS is doing it the same way, see this method in the Core RoutingServiceProvider that loads the backend routes:
您可以在服务提供商中加载自定义路由文件。AsgardCMS 也是这样做的,请参阅加载后端路由的 Core RoutingServiceProvider 中的此方法:
https://github.com/AsgardCms/Core/blob/master/Providers/RoutingServiceProvider.php#L77
https://github.com/AsgardCms/Core/blob/master/Providers/RoutingServiceProvider.php#L77
The Laravel docs provide a simple example in the package development section:
Laravel 文档在包开发部分提供了一个简单的示例:
回答by Mahmoud Zalt
create 2 route files
routes.web.php
androutes.api.php
.edit the
RouteServiceProvider.php
file to look like the example below:
创建 2 个路由文件
routes.web.php
和routes.api.php
.编辑
RouteServiceProvider.php
文件,使其看起来像下面的例子:
<?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');
});
}
}
Note: you can add as many route files as you want...
注意:您可以根据需要添加任意数量的路由文件...
回答by Benjamin Beganovi?
The group() method on Laravel's Route, can accept filename, so we can something like this:
Laravel 的 Route 上的 group() 方法可以接受文件名,所以我们可以这样:
// web.php
Route::prefix('admin')
->group(base_path('routes/admin.php'));
// admin.php
Route::get('/', 'AdminController@index');
回答by Silwerclaw
You can create as many route files as you need anywhere and then just require them in the main route file smth like:
您可以根据需要在任何地方创建任意数量的路由文件,然后只需要在主路由文件中使用它们,例如:
Route::get('/', function () {
return 'Hello World';
});
Route::post('foo/bar', function () {
return 'Hello World';
});
require_once "../../myModule1/routes.php";
require_once "../../myModule2/routes.php"
require_once "some_other_folder/routes.php"
where you will define routes in the same way as in main
您将以与 main 相同的方式定义路由
回答by Alex
回答by ctf0
in case someone still after that
万一有人在那之后
https://ctf0.wordpress.com/2016/10/01/split-routes-into-categories-in-laravel/
https://ctf0.wordpress.com/2016/10/01/split-routes-into-categories-in-laravel/
1- open routes/web/api.php and add
1- 打开 routes/web/api.php 并添加
foreach (File::allFiles(__DIR__ . '/Routes') as $route_file) {
require $route_file->getPathname();
}
2- now create a new folder at the same level and call it ‘Routes‘
2-现在在同一级别创建一个新文件夹并将其命名为“Routes”
3- create files according to each route ex.user, post, stuff, etc… and add ur route logic as normal
3- 根据每个路由创建文件,例如用户、帖子、东西等……并像往常一样添加你的路由逻辑
回答by Jones Pereira
My simple and fast solution for multiple route files. It works!
我对多个路由文件的简单快速的解决方案。有用!
In web.php file, add:
在 web.php 文件中,添加:
foreach (glob(__DIR__. '/*') as $router_files){
(basename($router_files =='web.php')) ? : (require_once $router_files);
}
回答by Mortada Jafar
laravel ^6
拉拉维尔^6
explained in comments
在评论中解释
<?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 diractory of your controllers
->group(base_path('routes/custom.php')); // change custom.php to your custom routers file name
}
}
回答by Jayson
If you just want to create a custom route file you can easily add your custom route file and register by using the web middleware name. The code is pretty simple like this.
如果您只想创建自定义路由文件,您可以轻松添加自定义路由文件并使用 Web 中间件名称进行注册。代码很简单,就像这样。
Edit App\Provider\RouteServiceProvider.php
编辑 App\Provider\RouteServiceProvider.php
public function map()
{
/** Insert this Method Name **/
$this->methodicalness();
}
protected function methodicalness()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/yourRouteName.php'));
}
Note: This method also works even the file is inside the folder just trace where your route file located.
注意:即使文件位于文件夹内,此方法也适用,只需跟踪您的路由文件所在的位置。
Happy Coding.
快乐编码。