我有一个有趣的情况。我已经安装了caffeinated modules并创建了一个名为Blog(slug,blog)的模块。有一个包含
的默认路由文件Route::group(['prefix' => 'blog'], function()
{
Route::get('/', function()
{
dd('This is the Blog module index page.');
});
});
因此,如果我转到http://localhost:8000/blog
,我希望看到该文字,但路线不匹配。如果我在那里添加任何其他内容,它就可以了。例如,我添加了
Route::get('/test', function()
{
dd('Test.');
});
到路线组,当我去http://localhost:8000/blog/test
时,我看到了测试。但是/ blog仍然无效。
现在事情变得更加奇怪了,我将路由组添加到我的默认路由文件中:
Route::group(['prefix' => 'blog'], function ()
{
Route::get('/', function ()
{
dd('test');
});
});
这条路线很有效。当我现在转到http://localhost:8000/blog
时,我会看到模块中路线的原始文字"This is the Blog module index page."
为什么在主app/Http/routes
文件中再次添加组会导致Blogs模块中的路由突然开始工作?为什么博客模块中的路由不起作用?
我想知道这个模块是否只是一个问题,所以我尝试创建另一个模块以进行测试,但是我使用另一个名称的模块获得了完全相同的结果。
模块和路由列表正确,这是module:list
和route:list
的输出,同时发生错误。
模块
+------+----------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| # | Name | Slug | Description | Status |
+------+----------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| 9001 | Menu | menu | Standard bootstrap menu. | Enabled |
| 9001 | Module Manager | modman | Manages modules installed in the site, holding the name and namespace of the module. It also determines which area a module is for (e.g. content, utility, stats etc.) | Enabled |
| 9001 | Image | image | A content area for a single static image. | Enabled |
| 9001 | Content | content | Core content module that uses a simple text area and a WYSIWYG editor. | Enabled |
| 9001 | Category | category | Creates different categories for use with blog posts | Enabled |
| 9001 | CommonArea | common-area | Common areas are groups of content that are repeated on multiple pages. The two default areas are header and footer. | Enabled |
| 9001 | Blog | blog | Handles blog articles as pages and has blog preview and menu content types. | Enabled |
+------+----------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
路线
+--------+--------------------------------+-------------------------------------+--------------------------+--------------------------------------------------------------------------+------------------------------------------------------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+--------------------------------+-------------------------------------+--------------------------+--------------------------------------------------------------------------+------------------------------------------------------------+
| | GET|HEAD | acp | AcpHome | App\Http\Controllers\AcpHomeController@index | web,auth |
| | GET|HEAD | acp/blog | AcpBlogPosts | App\Http\Controllers\AcpBlogController@index | web,auth |
| | POST | acp/blog/create | AcpCreateBlog | App\Http\Controllers\AcpBlogController@postCreate | web,auth |
| | GET|HEAD | acp/blog/create | AcpCreateBlogForm | App\Http\Controllers\AcpBlogController@getCreate | web,auth |
| | GET|HEAD | acp/blog/{blog}/delete | AcpDeleteBlog | App\Http\Controllers\AcpBlogController@delete | web,auth |
| | GET|HEAD | acp/blog/{blog}/update | AcpUpdateBlogForm | App\Http\Controllers\AcpBlogController@getUpdate | web,auth |
| | POST | acp/blog/{blog}/update | AcpUpdateBlog | App\Http\Controllers\AcpBlogController@postUpdate | web,auth |
| | GET|HEAD | acp/category | AcpCategories | App\Http\Controllers\AcpCategoryController@index | web,auth |
| | POST | acp/category/create | AcpCreateCategory | App\Http\Controllers\AcpCategoryController@postCreate | web,auth |
| | GET|HEAD | acp/category/create | AcpCreateCategoryForm | App\Http\Controllers\AcpCategoryController@getCreate | web,auth |
| | GET|HEAD | acp/category/{category}/delete | AcpDeleteCategory | App\Http\Controllers\AcpCategoryController@delete | web,auth |
| | POST | acp/category/{category}/update | AcpUpdateCategory | App\Http\Controllers\AcpCategoryController@postUpdate | web,auth |
| | GET|HEAD | acp/category/{category}/update | AcpUpdateCategoryForm | App\Http\Controllers\AcpCategoryController@getUpdate | web,auth |
| | GET|HEAD | acp/content | AcpContent | App\Http\Controllers\AcpContentController@index | web,auth |
| | GET|HEAD | acp/content-area/{content} | AcpContentAreaForm | App\Http\Controllers\AcpContentController@getArea | web,auth |
| | POST | acp/content-area/{content} | AcpContentArea | App\Http\Controllers\AcpContentController@postArea | web,auth |
| | GET|HEAD | acp/content/menus | AcpContentMenus | App\Http\Controllers\AcpContentController@index | web,auth |
| | GET|HEAD | acp/page/column-layout/{template} | AcpGetColumnLayout | App\Http\Controllers\AcpPageController@columnLayout | web,auth |
| | POST | acp/page/create | AcpCreatePage | App\Http\Controllers\AcpPageController@postCreate | web,auth |
| | GET|HEAD | acp/page/create | AcpCreatePageForm | App\Http\Controllers\AcpPageController@getCreate | web,auth |
| | GET|HEAD | acp/page/new-row | AcpGetNewRow | App\Http\Controllers\AcpPageController@newRow | web,auth |
| | POST | acp/page/{id}/update | AcpUpdatePage | App\Http\Controllers\AcpPageController@postUpdate | web,auth |
| | GET|HEAD | acp/page/{id}/update | AcpUpdatePageForm | App\Http\Controllers\AcpPageController@getUpdate | web,auth |
| | GET|HEAD | acp/permissions | AcpPermissions | App\Http\Controllers\AcpPermissionsController@index | web,auth |
| | POST | acp/permissions/create | AcpPermissionsCreate | App\Http\Controllers\AcpPermissionsController@postCreate | web,auth |
| | GET|HEAD | acp/permissions/create | AcpPermissionsCreateForm | App\Http\Controllers\AcpPermissionsController@getCreate | web,auth |
| | GET|HEAD | acp/permissions/{permission}/delete | AcpPermissionsDelete | App\Http\Controllers\AcpPermissionsController@delete | web,auth |
| | POST | acp/permissions/{permission}/update | AcpPermissionsUpdate | App\Http\Controllers\AcpPermissionsController@postUpdate | web,auth |
| | GET|HEAD | acp/permissions/{permission}/update | AcpPermissionsUpdateForm | App\Http\Controllers\AcpPermissionsController@getUpdate | web,auth |
| | GET|HEAD | acp/roles | AcpRoles | App\Http\Controllers\AcpRolesController@index | web,auth |
| | POST | acp/roles/create | AcpRolesCreate | App\Http\Controllers\AcpRolesController@postCreate | web,auth |
| | GET|HEAD | acp/roles/create | AcpRolesCreateForm | App\Http\Controllers\AcpRolesController@getCreate | web,auth |
| | GET|HEAD | acp/roles/{role}/delete | AcpRolesDelete | App\Http\Controllers\AcpRolesController@delete | web,auth |
| | POST | acp/roles/{role}/update | AcpRolesUpdate | App\Http\Controllers\AcpRolesController@postUpdate | web,auth |
| | GET|HEAD | acp/roles/{role}/update | AcpRolesUpdateForm | App\Http\Controllers\AcpRolesController@getUpdate | web,auth |
| | GET|HEAD | acp/templates | AcpTemplates | App\Http\Controllers\AcpTemplatesController@index | web,auth,advanced |
| | GET|HEAD | acp/templates/create | AcpTemplatesCreateForm | App\Http\Controllers\AcpTemplatesController@getCreate | web,auth,advanced |
| | POST | acp/templates/create | AcpTemplatesCreate | App\Http\Controllers\AcpTemplatesController@postCreate | web,auth,advanced |
| | GET|HEAD | acp/templates/{template}/delete | AcpTemplatesDelete | App\Http\Controllers\AcpTemplatesController@delete | web,auth,advanced |
| | POST | acp/templates/{template}/update | AcpTemplatesUpdate | App\Http\Controllers\AcpTemplatesController@postUpdate | web,auth,advanced |
| | GET|HEAD | acp/templates/{template}/update | AcpTemplatesUpdateForm | App\Http\Controllers\AcpTemplatesController@getUpdate | web,auth,advanced |
| | GET|HEAD | acp/users | AcpUsers | App\Http\Controllers\AcpUsersController@index | web,auth |
| | POST | acp/users/create | AcpUsersCreate | App\Http\Controllers\AcpUsersController@postCreate | web,auth |
| | GET|HEAD | acp/users/create | AcpUsersCreateForm | App\Http\Controllers\AcpUsersController@getCreate | web,auth |
| | GET|HEAD | acp/users/{user}/delete | AcpUsersDelete | App\Http\Controllers\AcpUsersController@delete | web,auth |
| | GET|HEAD | acp/users/{user}/update | AcpUsersUpdateForm | App\Http\Controllers\AcpUsersController@getUpdate | web,auth |
| | POST | acp/users/{user}/update | AcpUsersUpdate | App\Http\Controllers\AcpUsersController@postUpdate | web,auth |
| | GET|HEAD | blog | | Closure | web |
| | GET|HEAD | blog/test | | Closure | web |
| | GET|HEAD | category | | Closure | web |
| | GET|HEAD | common-area | | Closure | |
| | GET|HEAD | content | | Closure | |
| | GET|HEAD | image | | Closure | web |
| | GET|HEAD | laravel-filemanager | | Unisharp\Laravelfilemanager\controllers\LfmController@show | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/crop | | Unisharp\Laravelfilemanager\controllers\CropController@getCrop | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/cropimage | | Unisharp\Laravelfilemanager\controllers\CropController@getCropimage | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/delete | | Unisharp\Laravelfilemanager\controllers\DeleteController@getDelete | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/deletefolder | | Unisharp\Laravelfilemanager\controllers\FolderController@getDeletefolder | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/doresize | | Unisharp\Laravelfilemanager\controllers\ResizeController@performResize | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/download | | Unisharp\Laravelfilemanager\controllers\DownloadController@getDownload | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/folders | | Unisharp\Laravelfilemanager\controllers\FolderController@getFolders | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/jsonitems | | Unisharp\Laravelfilemanager\controllers\ItemsController@getItems | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/newfolder | | Unisharp\Laravelfilemanager\controllers\FolderController@getAddfolder | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/rename | | Unisharp\Laravelfilemanager\controllers\RenameController@getRename | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | laravel-filemanager/resize | | Unisharp\Laravelfilemanager\controllers\ResizeController@getResize | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD|POST|PUT|PATCH|DELETE | laravel-filemanager/upload | | Unisharp\Laravelfilemanager\controllers\UploadController@upload | web,auth,\Unisharp\Laravelfilemanager\middleware\MultiUser |
| | GET|HEAD | login | | App\Http\Controllers\Auth\AuthController@showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\AuthController@login | web,guest |
| | GET|HEAD | logout | | App\Http\Controllers\Auth\AuthController@logout | web |
| | GET|HEAD | menu | | Closure | |
| | GET|HEAD | modman | | Closure | web |
| | POST | password/email | | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\PasswordController@reset | web,guest |
| | GET|HEAD | password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController@showResetForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\AuthController@register | web,guest |
| | GET|HEAD | register | | App\Http\Controllers\Auth\AuthController@showRegistrationForm | web,guest |
| | GET|HEAD | {page?} | Page | App\Http\Controllers\PageController@index | web |
+--------+--------------------------------+-------------------------------------+--------------------------+--------------------------------------------------------------------------+------------------------------------------------------------+
答案 0 :(得分:0)
抱歉,我无法发表评论,但php artisan route:list
显示任何有用的内容,即。您是否看到了/blog
路线或/blog/
。
答案 1 :(得分:0)
您是否尝试过在 / 路线之前添加额外路线?
Route::group(['prefix' => 'blog'], function ()
{
Route::get('/check', function ()
{
dd('check');
});
Route::get('/', function ()
{
dd('test');
});
});
试试这个。希望对你有用。
答案 2 :(得分:0)
您的路线已在route:list
输出中注明。
我可以看到的唯一问题是,当您访问{page?}
时,有一条blog
路由,如果在/blog
之前定义,则会被调用。
在您的网页路线上尝试dd()
。
答案 3 :(得分:0)
组属性数组中的namespace 参数指定 组内所有控制器的命名空间
Route::group(['namespace' => 'blog', 'prefix' => 'blog'], function()
{
Route::get('/', function()
{
dd('This is the Blog module index page.');
});
});
答案 4 :(得分:0)
路线 {page?} 在模块之前执行,并与模块中的一个路段匹配。
运行此路线以查看路线的顺序
Route::get('routes', function() {
$router = $this->app->make('router');
$routes = $router->getRoutes();
foreach($routes AS $route)
{
dump($route->uri());
}
dd($routes);
});
路线:
Route::get('{slug?}', 'PageController@getPage')
->name('page')
->where('slug', '[a-zA-Z0-9_]*');
在我的应用程序中,我创建了文件 App / Routing / Matching / SlugValidator.php
<?php namespace App\Routing\Matching;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Routing\Matching\ValidatorInterface;
use App\Models\Page;
class SlugValidator implements ValidatorInterface
{
/**
* Validate a given rule against a route and request.
*
* @param \Illuminate\Routing\Route $route
* @param \Illuminate\Http\Request $request
* @return bool
*/
public function matches(Route $route, Request $request)
{
if ($route->getName() == 'page') {
return (bool) $page = Page::where('slug', '=', $request->segment(1))
->where('status', '=', PAGE::STATUS_ACTIVE)->count();
}
return true;
} // matches()
} // SlugValidator
在启动函数的 App / Http / Providers / RouteServiceProvider.php 中:
use Illuminate\Routing\Route as R;
use App\Routing\Matching\SlugValidator;
[ ....]
public function boot()
{
R::$validators = array_merge(R::getValidators(), [new SlugValidator]);
parent::boot();
}