控制器返回应用程序/ json而不是视图
我有一个非常奇怪的问题。我可以访问该页面,一切都很好,直到我在我的web.php路由文件中添加了一些新路由。问题在于第5条路线(名称为post.create)。该**只是突出线/路由我说的是:控制器返回应用程序/ json而不是视图
Route::group(['prefix'=>'admin', 'middleware'=>'auth'], function()
{
Route::get('home', '[email protected]')->name('admin.home');
Route::get('post/all','[email protected]')->name("post.all");
Route::get('post/{id?}','[email protected]')->name('post.fetch');
**Route::get('post/create','[email protected]')->name('post.create');**
Route::post('post/store', '[email protected]')->name('post.store');
Route::put('post/{id?}','[email protected]')->name('post.update');
Route::delete('post/delete/{id}','[email protected]')->name('post.delete');
Route::get('category/create','[email protected]')->name('category.create');
Route::post('category/store','[email protected]')->name('category.store');
Route::get('category/all','[email protected]')->name('category.all');
Route::get('category/{id?}','[email protected]')->name('category.fetch');
Route::delete('category/delete/{id}','[email protected]')->name('category.delete');
Route::put('category/{id}','[email protected]')->name('category.update');
});
当我访问这条路我得到了一个空白页面有一对大括号而已,没别的。浏览器控制台上显示一条消息 - 将资源解释为Document,但是使用MIME类型application/json进行传输。
但是,如果我的路线更改为
Route::get('posts/create','[email protected]')->name('post.create');
,这只是增加一个额外的小号,我得到的页面的完整视图。
我似乎无法弄清楚为什么早期的路由发回应用程序/ JSON(似乎是一个空对象)。我没有改变控制器的功能。下面是PostsController代码@创建功能:
public function create()
{
$categories = Category::all();
return view('admin.posts.create', compact('categories'));
}
我试图从这个函数返回一个不同的视图或简单的字符串这条路线。似乎没有任何工作。
我做错了什么,任何人都可以请帮忙吗?
resources/views/admin/posts/create.blade.php
刀片视图文件使用
.blade.php
文件扩展名通常存储在resources/views
目录
https://laravel.com/docs/5.4/blade#introduction
更新:
Laravel将按照您定义的顺序提供匹配的第一条路线。由于您首先拥有post.fetch
,因此使用'create'作为id
参数的路线。
在你的路由文件放在post.create
post.fetch
,因此,您有:
Route::get('post/create','[email protected]')->name('post.create');
Route::get('post/{id?}','[email protected]')->name('post.fetch');
Route::post('post/store', '[email protected]')->name('post.store');
Route::put('post/{id?}','[email protected]')->name('post.update');
,你应该命名刃文件
在评论中,我建议你在'post/{id?}'
之前移动路线。
刀片文件已经命名create.blade.php。正如我所说,如果我在定义中更改路由网址,即使是1个字符,也可以看到相同的页面。是的,这个文件存储在resources/views/admin/posts /目录下! –
试着把这条路线放在''post/{id?}''之一 –
嘿亚历克斯。谢谢,这是真的。 –
@Eric_Tucker:工作。非常感谢解释。因遇到问题而学习新事物:) –