Laravel 7 为什么使用 post 数据显示状态码 302
Laravel 7 why show status code 302 with post data
我使用 laravel 7 post 我的数据 bookcontroller@insertbook。
我发现用 302 显示状态代码。
但是我已经检查了我的 web.php 和控制器,它们没有错误出来。
the status code show
下面显示我的web.php代码
Route::redirect('/' , 'cn');
Route::get('dashboard', function () {
return redirect()->route('home' , ['language' => app()->getLocale() ?? 'cn']);
});
Route::group([
'prefix' => '{language}' ,
'where' => ['language' => '[a-z]{2}']
], function(){
Route::get('/', function () {
return view('auth.login');
})->name('login');
Auth::routes();
Route::group(['middleware' => ['auth']], function(){
Route::get('allbook', [
'uses' =>'BookController@listBook',
'as' => 'book_index'
]);
Route::get('dashboard', 'HomeController@index')->name('home');
Route::get('addcategory', 'BookController@categoryPage')->name('addCategory');
Route::post('addcategory','BookController@addnewCategory');
Route::get('addbook', 'BookController@addBook')->name('addbook');
Route::get('profile',function(){
return view('book.profile');
});
Route::get('book/edit/{id}', [
'uses' =>'BookController@edit',
'as' => 'book_edit'
]);
这是我的 BookController@insertBook
Route::post('addbook','BookController@insertBook');
Route::post('book/update/{id}', [
'uses' =>'BookController@update',
'as' => 'book_update'
]);
Route::post('book/delete/{id}', [ // this is the directory show in url
'uses' => 'BookController@delete', // this is get the BookController @delete method
'as' => 'book_delete' // this is the value you can use "route('book_delete') to run it post"
]);
});
});
下面是我的 bookcontroller 和 insertbook 功能
public function insertBook(Request $request)
{
$request->validate([
'title-name' => 'required|min:3|max:100',
'author-name' => 'required|regex:/^[\pL\s\-]+$/u',
'description' => 'required|min:10|max:300',
'category' => 'required',
'status' => 'required',
]);
$data = $request->input();
$books = new Books;
$books->title = $data['title-name'];
$books->author = $data['author-name'];
$books->description = $data['description'];
$books->category = $data['category'];
$books->status = $data['status'];
$books->save();
$request->session()->flash('message', 'You have successfully added a book!');
return back();
}
正如您在 laravel 文档中看到的 https://laravel.com/docs/7.x/redirects back() 方法是一种 http 重定向方法。 http 响应代码 302 是此辅助方法的默认行为,如您在供应商目录中所见:
vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
或在此处执行:
if (! function_exists('back')) {
/**
* Create a new redirect response to the previous location.
*
* @param int $status
* @param array $headers
* @param mixed $fallback
* @return \Illuminate\Http\RedirectResponse
*/
function back($status = 302, $headers = [], $fallback = false)
{
return app('redirect')->back($status, $headers, $fallback);
}
}
如果您想了解有关 HTTP 响应代码的更多信息:
您可能会发现此 link 有帮助:
The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header
return 302
状态代码是正确的,因为您正在 return 使用 back()
重定向到先前目的地的方法。
我使用 laravel 7 post 我的数据 bookcontroller@insertbook。 我发现用 302 显示状态代码。 但是我已经检查了我的 web.php 和控制器,它们没有错误出来。 the status code show
下面显示我的web.php代码
Route::redirect('/' , 'cn');
Route::get('dashboard', function () {
return redirect()->route('home' , ['language' => app()->getLocale() ?? 'cn']);
});
Route::group([
'prefix' => '{language}' ,
'where' => ['language' => '[a-z]{2}']
], function(){
Route::get('/', function () {
return view('auth.login');
})->name('login');
Auth::routes();
Route::group(['middleware' => ['auth']], function(){
Route::get('allbook', [
'uses' =>'BookController@listBook',
'as' => 'book_index'
]);
Route::get('dashboard', 'HomeController@index')->name('home');
Route::get('addcategory', 'BookController@categoryPage')->name('addCategory');
Route::post('addcategory','BookController@addnewCategory');
Route::get('addbook', 'BookController@addBook')->name('addbook');
Route::get('profile',function(){
return view('book.profile');
});
Route::get('book/edit/{id}', [
'uses' =>'BookController@edit',
'as' => 'book_edit'
]);
这是我的 BookController@insertBook
Route::post('addbook','BookController@insertBook');
Route::post('book/update/{id}', [
'uses' =>'BookController@update',
'as' => 'book_update'
]);
Route::post('book/delete/{id}', [ // this is the directory show in url
'uses' => 'BookController@delete', // this is get the BookController @delete method
'as' => 'book_delete' // this is the value you can use "route('book_delete') to run it post"
]);
});
});
下面是我的 bookcontroller 和 insertbook 功能
public function insertBook(Request $request)
{
$request->validate([
'title-name' => 'required|min:3|max:100',
'author-name' => 'required|regex:/^[\pL\s\-]+$/u',
'description' => 'required|min:10|max:300',
'category' => 'required',
'status' => 'required',
]);
$data = $request->input();
$books = new Books;
$books->title = $data['title-name'];
$books->author = $data['author-name'];
$books->description = $data['description'];
$books->category = $data['category'];
$books->status = $data['status'];
$books->save();
$request->session()->flash('message', 'You have successfully added a book!');
return back();
}
正如您在 laravel 文档中看到的 https://laravel.com/docs/7.x/redirects back() 方法是一种 http 重定向方法。 http 响应代码 302 是此辅助方法的默认行为,如您在供应商目录中所见:
vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
或在此处执行:
if (! function_exists('back')) {
/**
* Create a new redirect response to the previous location.
*
* @param int $status
* @param array $headers
* @param mixed $fallback
* @return \Illuminate\Http\RedirectResponse
*/
function back($status = 302, $headers = [], $fallback = false)
{
return app('redirect')->back($status, $headers, $fallback);
}
}
如果您想了解有关 HTTP 响应代码的更多信息:
您可能会发现此 link 有帮助:
The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header
return 302
状态代码是正确的,因为您正在 return 使用 back()
重定向到先前目的地的方法。