1

我有很多这样的路线:

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
4

3 回答 3

1

你需要写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');
         });

});
于 2018-09-28T07:22:43.720 回答
0

我正在使用这样的组路由。

<?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');
    });
});

尝试这个。可能这会对你有所帮助。

于 2018-09-28T07:20:50.517 回答
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重定向到确切的方法。

于 2018-09-28T17:38:51.313 回答