Problems in Laravel Resources 5.4 within Groups

1

I have problems trying to access some routes that come from a resource ( products ), it is inside a group, it follows my code:

Route::group([
    'middleware' => ['web', 'auth'],
    'prefix' => Config::get('shop.admin.url') . '/products',
    'namespace' => 'LaraShop\Products\Http\Controllers',
    'as' => 'products'
], function()
{
    Route::resource('/', 'ProductsController');
});

Routes generated:

| POST      | admin/products         | store   | LaraShop\Products\Http\Controllers\ProductsController@store   | auth
| GET|HEAD  | admin/products         | index   | LaraShop\Products\Http\Controllers\ProductsController@index   | auth
| GET|HEAD  | admin/products/create  | create  | LaraShop\Products\Http\Controllers\ProductsController@create  | auth
| DELETE    | admin/products/{}      | destroy | LaraShop\Products\Http\Controllers\ProductsController@destroy | auth
| PUT|PATCH | admin/products/{}      | update  | LaraShop\Products\Http\Controllers\ProductsController@update  | auth
| GET|HEAD  | admin/products/{}      | show    | LaraShop\Products\Http\Controllers\ProductsController@show    | auth
| GET|HEAD  | admin/products/{}/edit | edit    | LaraShop\Products\Http\Controllers\ProductsController@edit    | auth

When accessing the path admin/products/1/edit it returns error 404.

Has anyone ever been through this?

    
asked by anonymous 27.07.2017 / 21:49

2 answers

0

The routes resources follow a nomenclature like this:

First parameter is route name and the second class controller , in which case a slash, that is, the site root, is conflicting and also the concept is wrong, if it can not create such a direct route in the root, only in rare cases where this will be necessary, then, in place of the bar put " products " as exemplified below:

Route::group([
    'middleware' => ['web', 'auth'],
    'prefix' => Config::get('shop.admin.url') . '/products',
    'namespace' => 'LaraShop\Products\Http\Controllers',
    'as' => 'products'
], function()
{
    Route::resource('products', 'ProductsController');
});

This will solve your problem of not finding the route admin/products and at the time of printing the table by the command php artisan route:list note that inside the keys has text, can not come empty if not your route is in trouble, example:

+--------+-----------+-------------------------------+------------------+
|        | GET|HEAD  | /                             |                  |
|        | GET|HEAD  | admin/products                | products.index   |
|        | POST      | admin/products                | products.store   |
|        | GET|HEAD  | admin/products/create         | products.create  |
|        | GET|HEAD  | admin/products/{product}      | products.show    |
|        | PUT|PATCH | admin/products/{product}      | products.update  |
|        | DELETE    | admin/products/{product}      | products.destroy |
|        | GET|HEAD  | admin/products/{product}/edit | products.edit    |
+--------+-----------+-------------------------------+------------------+

27.07.2017 / 23:41
0

The simple solution to the case is:

route.php

Route::resource('admin/products', '\LaraShop\Products\Http\Controllers\ProductsController', [
    'as' => 'admin'
]);

ProductsController.php

class ProductsController extends Controller
{
    /**
     * Instantiate a new ProductsController instance.
     */
    public function __construct()
    {
        // defaults middlewares
        $this->middleware(['web', 'auth']);
    }

}

The simplest way and, in my opinion, the best semantically speaking.

    
28.07.2017 / 01:09