user13134426
user13134426

Reputation:

How to return back if request is get instead of post?

I have a route on which I submit a form. This route is Post type. when I directly put that url in address bar of browser it give me this error.

The GET method is not supported for this route. Supported methods: POST.

what I want is, it should return back instead of giving error.

Route:

Route::post('/warehouse-locations-import', 'WarehouseLocationController@warehouseLocationsImport')
        ->name('warehouselocations.import');

Controller:

//this function will extract all locations data from file
public function extractWarehouseLocations($locations)
{
    if ($locations['E'] && $locations['L'] && $locations['B'] && $locations['K']) {
        //making array of location data
        $locationArray = array(
            'name' => $locations['B'],
            'barcode' => $locations['C'],
            'group' => $locations['D'],
            'capacity' => $locations['E'],
            'row' => $locations['F'],
            'bay' => $locations['G'],
            'level' => $locations['H'],
            'depth' => $locations['I'],
            'product_type' => $locations['J'],
            'pick_efficiency' => $locations['K']
        );

        //adding index in array according to if condition
        $locations['L'] ? $locationArray['status'] = 1 : $locationArray['status'] = 0;
    }
}

Upvotes: 0

Views: 371

Answers (2)

M.Idrish
M.Idrish

Reputation: 437

Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the match method.

Route::match(['get', 'post'], '/', function () {
   //
});

Or, you may even register a route that responds to all HTTP verbs using the any method:

Route::any('/', function () {
   //
});

Upvotes: 2

Md Asaduzzaman
Md Asaduzzaman

Reputation: 99

You can show your from using get method and using post method you can send try using this,

Route::post('/warehouse-locations-import','WarehouseLocationController@warehouseLocationsImport')
            ->name('warehouselocations.import');
Route::get('/warehouse-locations-import', 'WarehouseLocationController@warehouseLocationsImportForm')
            ->name('warehouselocations.importform');

Upvotes: 0

Related Questions