一些背景:我已經將我的Laravel 5應用程序安裝到模塊中。在我AppServiceProvider引導()函數如下:Laravel的全球路線5
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));
}
}
}
}
的想法是能夠保持對事物的模塊分離,但也有全球性路線和全局控制器。因此,每個模塊都有自己的routes.php文件文件看起來是這樣的:
<?php
Route::group(array('module'=>'MyModule','namespace' => 'NexusHub\Modules\MyModule\Controllers'), function() {
Route::resource('mymodule', 'MyModuleController');
});
然後我有一個全球性的routes.php文件文件看起來像這樣:
<?php
Route::any('{catchall}', '[email protected]')->where('catchall', '(.*)');
Route::group(array('module'=>'Global'), function() {
Route::resource('', 'GlobalController');
});
我的問題碰到的是,似乎我的catchall路線沒有拿起模塊。模塊運行他們自己的路線,但超越路線被忽略。
至於爲什麼我試圖做到這一點,目前的目的是所有模塊使用相同的佈局,並且佈局需要總是檢索一些數據,所以全局控制器將抓住需要和做的它可用於佈局。但是我想在將來可能會有一些其他的東西,讓全局路由文件可以根據任意規則捕捉多條不同的路由,並運行額外的代碼就可以派上用場。
更新:因爲我意識到它們已經默認包含在內,所以刪除了包含全局路由的行。
包含模塊路由後,是否嘗試過包括全局路由? – nrivero
剛試過,沒關係。我也意識到,我甚至不需要那條線,無論如何,全局路由文件都被加載,包括app_path()。 '/Http/routes.php';線實際上不需要。如果我刪除它,全球路線仍然包含在模塊之後。 – Rocket04
根據你的描述,如果你只是試圖向_all_ routes/views注入一些數據,那麼它聽起來像視圖作曲家是一個更合適的解決方案。 –