Issues with Laravel Resources 5.4 within Groups

Asked

Viewed 212 times

1

I’m having trouble trying to access some routes that come from a Resource (products), She’s inside a group, 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 route admin/products/1/edit it returns error 404.

Someone’s been through it?

  • Your controller LaraShop\Products\Http\Controllers\ProductsController has the function edit? Try to put rum return 'ok';to see if you’re coming to the Controller...

  • It’s not coming to the controller, but when removing the Source from the group, it works normally.

  • And if you use Route::get('/{id}/edit', 'ProductsController'); inside your group, does it work? If it works inside the group and doesn’t work outside, see if you’re logged in and if your controller is using auth...

  • The best way I could find was to use Resource outside and middleware inside the controller.

  • is coming anyway admin/products/{} There’s nothing inside the keys???

2 answers

0

To routes resources follow a nomenclature like this:

First parameter is route name and the second to class controller, in your case if you put a bar, ie the root of the site, is conflicting and also the concept is wrong, if you can not create a route so direct at 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 when printing the table by command php artisan route:list note that inside the keys has a text, can not come empty if not your route is in trouble, correct 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    |
+--------+-----------+-------------------------------+------------------+

Reference:

  • Still it doesn’t work, that way nothing works.

  • @Raank problem should be your local because it must have route shocking that answer has been tested, if.?

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 way semantically speaking.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.