laravellaravel-routinglaravel-models

I got error with route defined in laravel-modules


In Laravel Framework 6.18.35 I use "nwidart/laravel-modules": "^7.2", and I have an error defining a route to method wrinnten in routes.

In Modules/Pages/Routes/web.php I have :

Route::prefix('pages')->group(function() {
    Route::get('/', 'PagesController@index');
    Route::resource('/admin/page_types', 'Admin\PageTypesController');
});

It refers to controller Modules/Pages/Http/Controllers/Admin/PageTypesController.php with method:

class PageTypesController extends Controller
{
    public function index()
    {
        \Log::info( '-1 PageTypesController index ::' . print_r(  -1, true  ) );

        return view('pages::admin.page_types.index');
    }

with template Modules/Pages/Resources/views/admin/page_types/index.blade.php, which has refrence:

<div class="col-12 col-sm-6 mb-3 mt-1 pl-2">
    <input type="submit" class="btn btn-primary" value="Search" onclick="javascript:backendPageType.runSearch(oTable); return false;" id="btn_run_search">
    <a onclick="javascript:document.location='{{ route('pages.admin.page_types.create') }}'" class="a_link">
        &nbsp;<small>&nbsp;&nbsp;&nbsp;&nbsp;( Add )</small>
    </a>
</div>

and this raise error :

Route [pages.admin.page_types.create] not defined. (View: /mnt/_work_sdb8/wwwroot/lar/lar6/Modules/Pages/Resources/views/admin/page_types/index.blade.php)

I tried several ways of setting the route nane, but failed.

Modified # 2: I see these routes:

php artisan route:list

Modules\Pages\Http\Controllers\PagesController@index             | web          |
|        | GET|HEAD  | pages/admin/{admin}/page_types                  | admin.page_types.index   | Modules\Pages\Http\Controllers\Admin\PageTypesController@index   | web          |
|        | POST      | pages/admin/{admin}/page_types                  | admin.page_types.store   | Modules\Pages\Http\Controllers\Admin\PageTypesController@store   | web          |
|        | GET|HEAD  | pages/admin/{admin}/page_types/create           | admin.page_types.create  | Modules\Pages\Http\Controllers\Admin\PageTypesController@create  | web          |
|        | GET|HEAD  | pages/admin/{admin}/page_types/{page_type}      | admin.page_types.show    | Modules\Pages\Http\Controllers\Admin\PageTypesController@show    | web          |
|        | PUT|PATCH | pages/admin/{admin}/page_types/{page_type}      | admin.page_types.update  | Modules\Pages\Http\Controllers\Admin\PageTypesController@update  | web          |
|        | DELETE    | pages/admin/{admin}/page_types/{page_type}      | admin.page_types.destroy | Modules\Pages\Http\Controllers\Admin\PageTypesController@destroy | web          |
|        | GET|HEAD  | pages/admin/{admin}/page_types/{page_type}/edit | admin.page_types.edit    | Modules\Pages\Http\Controllers\Admin\PageTypesController@edit    | web          |

I wonder why so many of "{admin}/" ? with content in Modules/Pages/Routes/web.php :

Route::prefix('pages')->group(function() {
    Route::get('/', 'PagesController@index');
    Route::resource('admin.page_types', 'Admin\PageTypesController');
});

Which is correct syntax?


Solution

  • To create nested resource, Laravel lets you use a dot notation for resource :

    Route::resource('admin.page_types', 'Admin\PageTypesController');
    

    To check registered routes by running php artisan route:list Which outputs a helpful table showing route names along with URLs and controller methods.

    Or, create another group for resource :

    Route::prefix('pages')->group(function() {
        Route::get('/', 'PagesController@index');
        Route::prefix('admin')->group(function() {
          Route::resource('page_types', 'Admin\PageTypesController');
      });
    });