Home > Net >  Laravel 7 why show status code 302 with post data
Laravel 7 why show status code 302 with post data

Time:04-10

I have using laravel 7 to post my data with bookcontroller@insertbook. And I have found show the status code with 302. But i have check my web.php and controller they is not error come out. the status code show

Below show my web.php code

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'
        ]);

Here is my 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"
        ]);

    });

});

Below is my bookcontroller with insertbook function

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();
    }

CodePudding user response:

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

It is true to return the 302 status code because you are returning the back() method which redirects to the previous destination.

CodePudding user response:

As you can see in the laravel docs https://laravel.com/docs/7.x/redirects the back() method is a http redirect method. The http response code 302 is the default behaviour for this helper method as you can see in your vendor directory:

vendor/laravel/framework/src/Illuminate/Foundation/helpers.php

or here in the implementation:

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);
    }
}

If you want to learn more about HTTP Response Codes:

You might find this link helpful:

https://umbraco.com/knowledge-base/http-status-codes/

  • Related