控制器返回 application/json 而不是视图

Controller returning application/json instead of view

我有一个很奇怪的问题。我可以访问该页面并且一切正常,直到我在 web.php 路由文件中添加了一些新路由。问题出在第 5 条路线(名为 post.create)。 ** 只是为了强调我正在谈论的 line/route:

Route::group(['prefix'=>'admin', 'middleware'=>'auth'], function()
{
Route::get('home', 'HomeController@index')->name('admin.home');

Route::get('post/all','PostsController@index')->name("post.all");

Route::get('post/{id?}','PostsController@show')->name('post.fetch');

**Route::get('post/create','PostsController@create')->name('post.create');**

Route::post('post/store', 'PostsController@store')->name('post.store');

Route::put('post/{id?}','PostsController@update')->name('post.update');

Route::delete('post/delete/{id}','PostsController@destroy')->name('post.delete');

Route::get('category/create','CategoriesController@create')->name('category.create');

Route::post('category/store','CategoriesController@store')->name('category.store');

Route::get('category/all','CategoriesController@index')->name('category.all');

Route::get('category/{id?}','CategoriesController@show')->name('category.fetch');

Route::delete('category/delete/{id}','CategoriesController@destroy')->name('category.delete');

Route::put('category/{id}','CategoriesController@update')->name('category.update');
});

当我访问这条路线时,我得到一个只有一对花括号的空白页,没有别的。浏览器控制台上有一条消息说 - Resource interpreted as Document but transferred with MIME type application/json.

但如果我将路线更改为

Route::get('posts/create','PostsController@create')->name('post.create');

,这只是添加了一个额外的 s,我得到了页面的完整视图。

我似乎无法弄清楚为什么较早的路由发回 application/json(似乎是一个空对象)。我没有改变控制器功能。这是 PostsController@create 函数的代码:

public function create()
{
    $categories = Category::all();
    return view('admin.posts.create', compact('categories'));
}

我已经尝试 return 不同的视图或此函数的简单字符串用于此路由。似乎没有任何效果。

我做错了什么,有人可以帮忙吗?

您应该将 Blade 文件命名为:

resources/views/admin/posts/create.blade.php

Blade view files use the .blade.php file extension and are typically stored in the resources/views directory

https://laravel.com/docs/5.4/blade#introduction

更新

在评论中,我建议您将路线移动到 'post/{id?}' 之前。

Laravel 将服务于按照您定义的顺序匹配的第一条路线。由于您首先有 post.fetch,因此它使用 'create' 作为 id 参数为该路由提供服务。

在你的路由文件中 post.create 放在 post.fetch 之前,所以你有:

Route::get('post/create','PostsController@create')->name('post.create');

Route::get('post/{id?}','PostsController@show')->name('post.fetch');

Route::post('post/store', 'PostsController@store')->name('post.store');

Route::put('post/{id?}','PostsController@update')->name('post.update');