Laravel redirect code 302 when submit form
use Validator::make to handle if yours validation if fails with custom errors response
see this link https://laravel.com/docs/8.x/validation#manually-creating-validators
Laravel unexpected redirects ( 302 )
After several hours of hair pulling, I have found my answer -- and it's silly.
The problem is that the route user.profile
has a path user/{uid?}
and it matches both user/logout
and user/add
as paths.
It being before the others, and not having a regex or similar, it handled the route.
I still don't know why a 302 was generated for that page, but found that moving it out of the AuthController
and into the UserController
(where it should be from the start) fixed the behavior.
Thus, my (amended and working) routes now look like so:
Route::group(['middleware' => 'web'], function () {
// Authentication Routes...
Route::get( 'user/login', ['as' => 'user.login', 'uses' => 'Auth\AuthController@showLoginForm']);
Route::post('user/login', ['as' => 'user.doLogin', 'uses' => 'Auth\AuthController@login' ]);
Route::group(['middleware' => 'auth'], function() {
// Authenticated user routes
Route::get( '/', ['as'=>'home', 'uses'=> 'HomeController@index']);
Route::get( '/home', ['as'=>'home', 'uses'=> 'HomeController@home']);
Route::get( 'user/logout', ['as' => 'user.logout', 'uses' => 'Auth\AuthController@logout' ]);
// *** Added /profile/ here to prevent matching with other routes ****
Route::get( 'user/profile/{uid?}', ['as' => 'user.profile', 'uses' => 'UserController@profile' ]);
Route::get( '/user/add', ['as' => 'user.add', 'uses' => 'UserController@showAddUser']);
[...]
});
});
Laravel 302 redirection when I do a POST
You have middleware 'auth'
for your route. You should investigate a bit on it and you'll understand why doesn't it work.
The point is that 'auth'
middleware requires cookies to work correctly, while you have it empty when you're have Ajax request. Why does it give you 302 and not 401? You should look into your authentication handler for any custom logic on that.
What to do? Implement JWT auth or stateless authentication!
Redirect 302 on Laravel POST request
it looks like in postman you should point that the data you send is 'x-www-url-formurlencoded'
Related Topics
How Should a Model Be Structured in MVC
How to Parameterize the Table Name in a Prepared Statement
How to Ping a Server Port With PHP
How to Fix "Headers Already Sent" Error in PHP
What Does the PHP Error Message "Notice: Use of Undefined Constant" Mean
PHP Code Is Not Being Executed, But the Code Shows in the Browser Source Code
How to Prevent Xss With Html/PHP
PHP Pass Variable to Next Page
Failed to Open the System Default Web Browser. Is One Installed - Pgadmin4
How to Use Return Inside a Recursive Function in PHP