我是使用Laravel 5开发Web的新手。我安装了asGgardCMS,看到asgardCms代码后,我发现app / Http / route.php文件中没有任何代码,路由所需的代码放在模块代码中。例如,路由菜单管理器模块的必需代码放在Modules / Media / apiRoutes.php和Modules / Media / backendRoutes.php文件中。可以帮助我,告诉我如何管理这样的路线?
答案 0 :(得分:9)
创建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');
});
}
}
注意:您可以根据需要添加任意数量的路径文件...
答案 1 :(得分:4)
您可以在任何地方创建任意数量的路径文件,然后只需要在主路径文件中使用它们,如:
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"
您将以与主
相同的方式定义路线答案 2 :(得分:3)
Laravel路线上的group()方法可以接受文件名,所以我们可以这样:
// web.php
Route::prefix('admin')
->group(base_path('routes/admin.php'));
// admin.php
Route::get('/', 'AdminController@index');
答案 3 :(得分:3)
laravel ^ 6
注释中的解释
<?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
}
}
答案 4 :(得分:2)
您可以在服务提供商中加载自定义路由文件。 AsgardCMS也是这样做的,在Core RoutingServiceProvider中看到加载后端路由的方法:
https://github.com/AsgardCms/Core/blob/master/Providers/RoutingServiceProvider.php#L77
Laravel文档在包开发部分提供了一个简单的例子:
答案 5 :(得分:2)
您可以使用Request::is()
,因此您的主routes.php
文件将如下所示:
if(Request::is('frontend/*))
{
require __DIR__.'/frontend_routes.php;
}
if(Request::is('admin/*))
{
require __DIR__.'/admin_routes.php;
}
您可以阅读更多here。
答案 6 :(得分:2)
以防有人在此之后
https://ctf0.wordpress.com/2016/10/01/split-routes-into-categories-in-laravel/
1-打开routes / web / api.php并添加
foreach (File::allFiles(__DIR__ . '/Routes') as $route_file) {
require $route_file->getPathname();
}
2-现在在同一级别创建一个新文件夹,并将其命名为“ Routes”
3-根据每条路线(例如用户,帖子,东西等)创建文件,并像往常一样添加您的路线逻辑
答案 7 :(得分:1)
如果您只想创建自定义路径文件,可以使用Web中间件名称轻松添加自定义路径文件并进行注册。代码非常简单。
编辑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'));
}
注意:即使文件位于路径文件所在的文件夹内,此方法也可以正常工作。
快乐编码。
答案 8 :(得分:0)
我对多个路由文件的简单快速解决方案。可行!
在web.php文件中,添加:
foreach (glob(__DIR__. '/*') as $router_files){
(basename($router_files =='web.php')) ? : (require_once $router_files);
}