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 false
in the authorize
method and it works correctly. If I fire a request, it says forbidden. But then I updated the rules and I set the authorize
method to true
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:
<?php
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:
X-Requested-With:XMLHttpRequest
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With