我有一个全局资源控制器,并通过以下方式建立路由:
Route::resource('cat', 'CategoryController');
在CategoryController
类中,destroy
方法是这样的:
public function destroy (\App\Category $cat)
{
$cat->delete();
return ['success' => true, 'msg' => 'Category removed'];
}
现在根据Docs about RESTful Resource Controllers destroy
应该可以回复通过 DELETE 方法发送的请求。但在这种情况下,它也可以响应通过 GET 方法发送的请求而这让我感到奇怪。
它会发生什么?
我正在使用laravel 5.3.4。
更新
这是我的完整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'];
}
}
完整的路线:
Route::resource('cat', 'CategoryController');
Route::group(
array (
'prefix' => 'admin',
'as' => 'admin.',
'middleware' => 'auth'
),
function () {
Route::group(['prefix' => 'post'], function () {
Route::resource('category', 'CategoryController');
});
});
});
因为你可以看到另一个资源控制器引用受保护路由组中名为CategoryController
的{{1}}。
答案 0 :(得分:1)
我认为CategoryController
上的2条资源路线存在冲突。
你能尝试用这种方式实现它们吗?
Route::resource('cat', 'CategoryController', ['except' => 'destroy']);
Route::delete('cat', ['as' => 'cat.destroy', 'uses' => 'CategoryController@destroy']);
另外,以你的方式实现路由非常麻烦,尽可能明确地编写它们以防止像你正在经历的那样奇怪的错误。