Laravel 拉维尔5号全球航线
一些背景:我已经将我的Laravel 5应用程序设置为分为多个模块。my AppServiceProvider中的boot()函数如下所示:Laravel 拉维尔5号全球航线,laravel,Laravel,一些背景:我已经将我的Laravel 5应用程序设置为分为多个模块。my AppServiceProvider中的boot()函数如下所示: public function boot() { // We want to register the modules in the Modules folder $modulesPath = app_path() . '/Modules'; $handle = opendir($modulesPath); //
public function boot()
{
// We want to register the modules in the Modules folder
$modulesPath = app_path() . '/Modules';
$handle = opendir($modulesPath);
// Loop through the module directory and register each module
while (($module = readdir($handle)) !== false) {
if ($module != "." && $module != ".." && is_dir("{$modulesPath}/{$module}")) {
// Check if there are routes for that module, if so include
if (file_exists($modulesPath . '/' . $module . '/routes.php')) {
include $modulesPath . '/' . $module . '/routes.php';
}
// Check if there are views for that module, if so set a namespace for those views
if (is_dir($modulesPath . '/' . $module . '/Views')) {
$this->loadViewsFrom($modulesPath . '/' . $module . '/Views', strtolower($module));
}
}
}
}
<?php
Route::group(array('module'=>'MyModule','namespace' => 'NexusHub\Modules\MyModule\Controllers'), function() {
Route::resource('mymodule', 'MyModuleController');
});
<?php
Route::any('{catchall}', 'GlobalController@myAction')->where('catchall', '(.*)');
Route::group(array('module'=>'Global'), function() {
Route::resource('', 'GlobalController');
});
其想法是能够在模块中保持事物的分离,但也有全局路由和全局控制器。因此,每个模块都有自己的routes.php文件,该文件如下所示:
public function boot()
{
// We want to register the modules in the Modules folder
$modulesPath = app_path() . '/Modules';
$handle = opendir($modulesPath);
// Loop through the module directory and register each module
while (($module = readdir($handle)) !== false) {
if ($module != "." && $module != ".." && is_dir("{$modulesPath}/{$module}")) {
// Check if there are routes for that module, if so include
if (file_exists($modulesPath . '/' . $module . '/routes.php')) {
include $modulesPath . '/' . $module . '/routes.php';
}
// Check if there are views for that module, if so set a namespace for those views
if (is_dir($modulesPath . '/' . $module . '/Views')) {
$this->loadViewsFrom($modulesPath . '/' . $module . '/Views', strtolower($module));
}
}
}
}
<?php
Route::group(array('module'=>'MyModule','namespace' => 'NexusHub\Modules\MyModule\Controllers'), function() {
Route::resource('mymodule', 'MyModuleController');
});
<?php
Route::any('{catchall}', 'GlobalController@myAction')->where('catchall', '(.*)');
Route::group(array('module'=>'Global'), function() {
Route::resource('', 'GlobalController');
});
首先加载全局路由文件
尝试将加载所有模块路由的服务提供商移动到config/App.php文件中的“App\Providers\RouteServiceProvider”之前
…看看这是否有帮助 我最终用中间件做了我想做的事情(请参阅)
在我的例子中,我使用了类似于BeforeMidleware类示例的东西,并在我的app/Http/Kernel.php类的$middleware属性中注册了它,因为它是全局的,不依赖于路由。在包含模块路由之后,您是否尝试过包含全局路由?只是尝试过,没关系。我还意识到,我甚至不需要那一行,全局路由文件都会被加载,不管是什么,include app_path()/Http/routes.php';这条线实际上是不需要的。如果我删除它,全局路由仍然会在模块路由之后包含。根据您的描述,如果您只是尝试向所有路由/视图注入一些数据,那么听起来视图生成器是更合适的解决方案。我们将研究视图生成器。我在Laravel文档中看到了基本描述,但不确定我将在哪里声明它们以及如何在布局中使用它们。我能够使用中间件使事情正常工作,但不确定这是否是最好的解决方案。请参阅我在上面对您的评论的评论,我尝试移动它,但它没有解决问题。我还从boot()函数中删除了全局路由的加载,因为我意识到全局路由仍在加载中,并且该文件包含在模块文件之后。MMmm。。。尝试在App::before应用程序筛选器中声明您的“一网打尽”路由。可以说我真的知道您所说的App::before筛选器是什么意思。但我得到的印象是,我试图做的事情对路线不起作用。看看Router类中的函数,似乎只有一条路径匹配。如果是真的,那么我可能只需要想出一种不同的方法。我相信有一种方法可以做到这一点,只要你最后注册了所有的内容。我希望“php artisan路由:列表”按注册顺序列出路由。我会尝试的一件事是将全球路线移动到一个服务提供商,并确保其在“提供商”阵列中的最后一个。您能总结一下您的解决方案吗?很好,我已经更新了答案,为人们指明了正确的方向。