Search code examples
phplaravellaravel-5.3

destroy method of a resource controller can respond to GET request method While should not be able to


I have a global resource controller and make a route from that like this:

Route::resource('cat', 'CategoryController');

In the CategoryController class is destroy method like this :

public function destroy (\App\Category $cat)
{
    $cat->delete();
    return ['success' => true, 'msg' => 'Category removed'];
}

Now According to Docs about RESTful Resource Controllers destroy should can respond to requests that sent via DELETE method. but in this case it can respond to requests that sent via GET method too And that makes me wonder.

What happens to it?

I'm using laravel 5.3.4.

Update : This is my Full CategoryController :

class CategoryController extends Controller
    {

        public function index ()
        {
            return view('admin/pages/post/category_manage');
        }

        public function create ()
        {

        }

        public function store (Request $request)
        {

            $result = ['success' => true, 'msg' => 'عملیات با موفقیت انجام شد'];

            $allData = $request->only('text', 'parent');

            $rules = array (
                'text'   => 'required|min:2',
                'parent' => 'required'
            );

            $validator = \Validator::make($allData, $rules);
            if ($validator->fails()) {

                $result = ['success' => false, 'msg' => $validator->errors()->first()];

            } else {

                if ($allData['parent'] == '#') {
                    $root = NULL;
                } else {
                    $root = Category::find($allData['parent']);
                }

                $newCategory = Category::create(['name' => $allData['text']]);
                if (is_null($root)) {
                    $newCategory->makeRoot();
                } else {
                    $newCategory->makeChildOf($root);
                }

                $result['generated_id'] = $newCategory->cat_id;

            }

            return $result;

        }

        public function show ($cat_alias)
        {

            return $cat_alias;
            $category = Category::whereCatAlias($cat_alias)->firstOrFail();

            $postCategory = Post::active()->scheduled()->whereHas('categories', function ($query) use ($cat_alias) {
                $query->whereCatAlias($cat_alias);
            })
                ->select(['post_title', 'post_sub_title', 'post_alias', 'start_date', 'end_date', 'created_at', 'hits', 'picture'])
                ->paginate(15);


//          return $postCategory;

            return view('main.pages.category', ['postsCategories' => $postCategory, 'category' => $category]);
        }

        public function edit ($id)
        {
            //
        }

        public function update ($id, Request $request)
        {
            $result = ['success' => true, 'msg' => 'عملیات با موفقیت انجام شد'];

            $allData = $request->only('text', 'old');
            $rules   = array (
                'text' => 'required|min:2',
                'old'  => 'required'
            );

            $validator = \Validator::make($allData, $rules);
            if ($validator->fails()) {
                $result = ['success' => false, 'msg' => $validator->errors()->first()];
            } else {
                $node        = Category::find($id);
                $newCategory = $node->update(['name' => $allData['text']]);
            }

            return $result;

        }

        public function destroy (\App\Category $cat)
        {
            $cat->delete();
            return ['success' => true, 'msg' => 'Category removed'];
        }
    }

And full Routes:

Route::resource('cat', 'CategoryController');
Route::group(
            array (
                'prefix' => 'admin',
                'as'     => 'admin.',
                'middleware' => 'auth'
            ),
            function () {
                   Route::group(['prefix' => 'post'], function () {         
                       Route::resource('category', 'CategoryController');
                    });
             });
});

As yu can see there is another resource controller refers to CategoryController named category in a protected route group.


Solution

  • I think the 2 resource routes on CategoryController are conflicting.

    Can you try to implement them this way?

    Route::resource('cat', 'CategoryController', ['except' => 'destroy']);
    Route::delete('cat', ['as' => 'cat.destroy', 'uses' => 'CategoryController@destroy']);
    

    Also, it's pretty messy to implement routes the way you did, try to write them as explicit as possible to prevent weird bugs like the one you're experiencing.