Rohan Rohan - 2 months ago 12
PHP Question

Why is my create form request throwing off a 404 exception in Laravel5?

So I am working with a Laravel 5 installation and like a good programmer I am trying to get the validation logic out of my controller using the new Form Requests feature in Laravel.

So I went ahead and created a form request called CreateTenantRequest like so:

php artisan make:request CreateTenantRequest

By default it returns a
in the
method and it works correctly. If I fire a request, it says forbidden. But then I updated the rules and I set the
method to
and now when I fire the same request from Postman, it says:

NotFoundHttpException in RouteCollection.php line 161:

Which is ridiculous because when I change it to false, it returns forbidden fine?

What am I missing or doing wrong?

And although this wouldn't matter I guess but my rules array is as follows:

return [
// Tenant details
'name' => 'required|max:255',
'username' => 'required|max:255|unique:tenant',
// Tenant Admin details
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email|max:255',
'password' => 'required|confirmed|min:6',

Routes file:


Route::group(['prefix' => 'api'], function(){
Route::post('authenticate', 'Auth\AuthController@authenticate');

// SuperAdmin Group
Route::group(['namespace' => 'Archive', 'middleware' => 'superadmin'], function(){
Route::resource('tenants', 'TenantController');
Route::get('tenants/{id}/users', 'TenantController@showUsersForTenant');

// Tenant Group
Route::group(['namespace' => 'Tenant'], function(){
Route::resource('roles', 'RoleController');
Route::resource('users', 'UserController');

// Remove before production
// Only for testing purposes
Route::get('test', function(){
// return JWTAuth::parseToken()->getPayload()->get('username');


So after a bid discussion with shock_gone_wild, I realized that the request was not ajax and hence laravel was rerouting to the a url with the errors in session.

I was testing the API with Postman REST client and by default it sends basic HTTP requests but when a header is added like so:


It makes the request Ajax and then laravel checks to see if it is indeed ajax, so instead of routing to a url, it gave back the validation errors in JSON.

So anytime if anyone is creating a decoupled web service and trying to test it using Postman, do it with the header so that you can simulate actual ajax requests at your application.