我有很多这样的路线:
Route::resource('/dashboard/class', 'classController');
Route::get('/dashboard/class/get-by-filter', 'classController@getByFilter');
Route::get('/dashboard/class/get-by-search', 'classController@getBySearch');
...
...
Route::resource('/dashboard/orders', 'OrderController');
Route::get('/dashboard/orders/get-by-filter', 'OrderController@getByFilter');
...
现在我想用前缀,组和资源来写这些,但是这样写时我有一个问题:
Route::prefix('dashboard')->group(function()
{
Route::prefix('class')->group(function()
{
Route::resource('/', 'classController');
Route::get('/get-by-filter', 'classController@getByFilter');
Route::get('/get-by-search', 'classController@getBySearch');
});
Route::prefix('orders')->group(function()
{
Route::resource('/', 'OrderController');
Route::get('/get-by-filter', 'OrderController@getByFilter');
Route::get('/get-by-search', 'OrderController@getBySearch');
});
});
当我尝试访问显示地址时为什么返回404:
example.com/dashboard/orders/4
答案 0 :(得分:1)
我正在使用这样的组路由。
<?php
Route::group(['prefix' => 'dashboard'], function() {
Route::group(['prefix' => 'class'], function() {
Route::resource('/', 'classController');
Route::get('/get-by-filter', 'classController@getByFilter');
Route::get('/get-by-search', 'classController@getBySearch');
});
Route::group(['prefix' => 'orders'], function() {
Route::resource('/', 'OrderController');
Route::get('/get-by-filter', 'OrderController@getByFilter');
Route::get('/get-by-search', 'OrderController@getBySearch');
});
});
尝试一下。可能会对您有帮助。
答案 1 :(得分:0)
您需要写resource
而不是get
Route::prefix('dashboard')->group(function()
{
Route::prefix('class')->group(function()
{
Route::resource('/', 'classController');
Route::get('/get-by-filter', 'classController@getByFilter');
Route::get('/get-by-search', 'classController@getBySearch');
});
Route::prefix('orders')->group(function()
{
Route::resource('/', 'OrderController');
Route::get('/get-by-filter', 'OrderController@getByFilter');
Route::get('/get-by-search', 'OrderController@getBySearch');
});
});
答案 2 :(得分:0)
好的,我想我明白了,您需要在URL中传递{id},然后在控制器方法中传递dd($ id)。
Route::group(['prefix'=>'dashboard'], function()
{
Route::group(['prefix'=>'class'], function()
{
Route::resource('/', 'classController');
Route::get('/get-by-filter', 'classController@getByFilter');
Route::get('/get-by-search', 'classController@getBySearch');
});
Route::group(['prefix'=>'orders'], function()
{
Route::resource('/{id}','classController');
Route::get('/get-by-filter', 'OrderController@getByFilter');
Route::get('/get-by-search', 'OrderController@getBySearch');
});
});
我建议您使用正确的名称空间,并确保将 classController 重定向到确切的方法。