简体   繁体   English

如何通过目录定义laravel 5.0路由

[英]how to define laravel 5.0 routes by means of directories

I would like to do this thing with laravel 5.0. 我想用laravel 5.0来做这件事。 I would have many routes.php files in some folders (like, under the app/ folder, I create a "linux" folder and a "windows" folder). 我将在某些文件夹中拥有许多route.php文件(例如,在app /文件夹下,我创建了“ linux”文件夹和“ windows”文件夹)。 Inside the two linux and windows folders, i put two routes.php files, with different routes (like, i don't know, they both have the route get('os') ). 在两个linux和windows文件夹里面,我放了两个routes.php文件,有不同的路由(比如,我不知道,他们都有路由get('os'))。 What I want is that calling public/windows/os it is called the route os of the windows folder, and calling public/linux/os it is called the route os of the linux folder (basic polimorphism). 我想要的是调用public / windows / os它被称为windows文件夹的路由os,并且调用public / linux / os它被称为linux文件夹的路由os(基本的polimorphism)。 I searched over and over and found nothing helpful. 我一遍又一遍地搜索,没有发现任何帮助。 Do you have some advice? 你有什么建议吗? Thanks. 谢谢。

You can simply include the other route files in your main routes.php and put them inside route groups: 您可以简单地将其他路由文件包含在主routes.php ,并将它们放入路由组中:

Route::group(['prefix' => 'windows'], function(){
    include app_path('windows/routes.php');
});

Route::group(['prefix' => 'linux'], function(){
    include app_path('linux/routes.php');
});

To reduce the duplicate code you can also use an array to configure your subdirectories: 要减少重复代码,您还可以使用数组来配置子目录:

$systems = ['windows', 'linux'];
foreach($systems as $os){
    Route::group(['prefix' => $os], function(){
        include app_path($os.'/routes.php');
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM