I am building a REST API using Laravel 5.1 and I am getting this error:
TokenMismatchException in VerifyCsrfToken.php line 53:
Here is my routes.php:
Route::controller('city' , 'CityController' );
CityController:
class CityController extends Controller
{
public function postLocalities()
{
$city = Input::get('cityName');
$response = $city;
return $response;
}
}
Here is the Stacktrace of the error when I hit the URL
http://localhost:8000/city/localities?cityName=bangalore with POST method.
TokenMismatchException in VerifyCsrfToken.php line 53:
in VerifyCsrfToken.php line 53
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'),
array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in
ShareErrorsFromSession.php line 54
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'),
array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in
StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'),
array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in
AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'),
array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'),
array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in
CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'),
array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\Users\betaworks02\Documents\gharbhezoBackend\public\index.php') in server.php line 21
If you are building an API its best to place the CRSF middle ware on per route basis rather than placing it as a global middleware. To make it as a route middleware go to the "/app/Http/Kernel.php" file.
/**
* The application's global HTTP middleware stack.
*
* #var array
*/
protected $middleware = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
//comment out to avoid CSRF Token mismatch error
// 'App\Http\Middleware\VerifyCsrfToken',
];
/**
* The application's route middleware.
*
* #var array
*/
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
'cors' => 'App\Http\Middleware\CorsMiddleware',
'api' => 'App\Http\Middleware\ApiMiddleware',
'csrf' => 'App\Http\Middleware\VerifyCsrfToken'// add it as a middleware route
Now you can place it on the routes where you need it for example
Route::get('someRoute', array('uses' => 'HomeController#getSomeRoute', 'middleware' => 'csrf'));
For your case where you don't need CSRF token matching it should work fine now.
You do not need to fully override the CFSR token from your app. In your App/Http/Midlleware folder go to VerifyCsrfToken.php and include your API route to the exception as follows:
/**
* The URIs that should be excluded from CSRF verification.
*
* #var array
*/
protected $except = [
'api/*',
];
The * shows for all routes inside your API.
I was getting the same error, but with all the warnings about overriding CSRF validation, didn't want to change those settings.
I eventually found that my Session Driver in /config/session.php was defaulting to memcached, and since I was on a development server I needed to override the SESSION_DRIVER env variable with 'file' to use the session in /storage/framework/sessions.
/.env
SESSION_DRIVER = file
Related
So i've search the farthest reaches of the internet and come up with nothing. Please help!
I am developing locally on my Mac, all is fine.
When I push the site to production (CentOS7 VPS) I get the following error:
BadMethodCallException in Validator.php line 3162:
Method [validatePublication] does not exist.
Here is my app/Services/Validation/CustomValidation.php
```
use Illuminate\Validation\Validator;
class CustomValidation extends Validator {
//added only for test
public function validateCartRow($attribute, $value, $parameters)
{
$valid = (is_null(\Cart::get($value))) ? false : true;
return $valid;
}
public function validatePublication($attribute, $value, $parameters)
{
$valid = (\PublisherAPI::publicationByID($value) == false) ? false : true;
return $valid;
}
}
```
Here is my app/Providers/ValidationServiceProvider.php
```
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\Validation\CustomValidation;
use Illuminate\Validation\Validator;
class ValidationServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
$this->app->validator->resolver(function($translator, $data, $rules, $messages)
{
return new \App\Services\Validation\CustomValidation($translator, $data, $rules, $messages);
});
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
```
Here is my app/Http/Requests/Cart/addToCartRequest.php
```
namespace App\Http\Requests\Cart;
use App\Http\Requests\Request;
class addToCartRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'row_publication_id' => 'required|string|numeric|publication',
'row_price_key' => 'required|string|numeric',
];
}
}
```
This is the request that is failing on production. Again, I stress this is not a problem on my local Mac.
Any thoughts? I'm stumped...
Thanks!
UPDATE: Stack trace...
in Validator.php line 3265
at Validator->__call('validatePublication', array('row_publication_id', '4', array(), object(Validator))) in Validator.php line 485
at Validator->validatePublication('row_publication_id', '4', array(), object(Validator)) in Validator.php line 485
at Validator->validate('row_publication_id', 'publication') in Validator.php line 425
at Validator->passes() in ValidatesWhenResolvedTrait.php line 24
at FormRequest->validate() in FoundationServiceProvider.php line 41
at FoundationServiceProvider->Illuminate\Foundation\Providers\{closure}(object(addToCartRequest), object(Application)) in Container.php line 1031
at Container->fireCallbackArray(object(addToCartRequest), array(object(Closure))) in Container.php line 996
at Container->fireResolvingCallbacks('App\Http\Requests\Cart\addToCartRequest', object(addToCartRequest)) in Container.php line 648
at Container->make('App\Http\Requests\Cart\addToCartRequest', array()) in Application.php line 697
at Application->make('App\Http\Requests\Cart\addToCartRequest') in RouteDependencyResolverTrait.php line 85
at ControllerDispatcher->transformDependency(object(ReflectionParameter), array(), array()) in RouteDependencyResolverTrait.php line 59
at ControllerDispatcher->resolveMethodDependencies(array(), object(ReflectionMethod)) in RouteDependencyResolverTrait.php line 42
at ControllerDispatcher->resolveClassMethodDependencies(array(), object(CartController), 'addToCart') in ControllerDispatcher.php line 144
at ControllerDispatcher->call(object(CartController), object(Route), 'addToCart') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(CartController), object(Route), object(Request), 'addToCart') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\Cart\CartController', 'addToCart') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SessionTimeout.php line 58
at SessionTimeout->handle(object(Request), object(Closure))
at call_user_func_array(array(object(SessionTimeout), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
Turns out it was a filename in app.php when registering the service provider.
I missed it as on local, the filename was correct. However I have a merge-driver setup that I completely forgot about preventing app.php being merged with staging (because of the application URL), so the error persisted.
I think I need to move the application URL to an environment variable and load the relevant config, getting rid of the merge driver.
Thanks to the guys at Laracasts for helping me get to the bottom of this..
https://laracasts.com/discuss/channels/laravel/error-on-production-not-on-local
I'm getting this error, while trying to access a page that I've added in my routes..
ex. laravel.com/about-us
here's a sample of my routes code.
I was asked to do the routes in config.php
'about-us' => [
'controller' => 'page',
'method' => 'about',
'enable' => true
],
and a method like this.
public function about() {
return view($this->getViewPrefix() . '::page.about');
}
The ful error:
NotFoundHttpException in Application.php line 879:
in Application.php line 879
at Application->abort('404', '', array()) in helpers.php line 21
at abort('404') in helpers.php line 45
at cd_abort('404') in routes.php line 93
at ServiceProvider->{closure}('about-us')
at call_user_func_array(object(Closure), array('param1' => 'about-us')) in Route.php line 155
at Route->runCallable(object(Request)) in Route.php line 130
at Route->run(object(Request)) in Router.php line 704
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
I personally don't think it's how I did these things, because one of my colleagues got two of these pages to work, (they only have two) ..
does this have to do with my installation, or maybe my OS lol.?
I don't think you can write routes in config file. You have to write your routes in this file app/Http/routes.php Write these lines
Route::get('about-us', [
'as' => 'aboutUs', 'uses' => 'PagesController#about'
]);
Write your controller's name instead of PagesController. You have to make your controller inside this folder app/Http/Controllers
Laravel has the routes.php file to contain all the application related routing. This file will be autoloaded when the application runs.
Therefore, include your routing in this file for best practice.
A "get" routing will be like:
Route::get('/about-us', [ 'as' => 'about', 'uses' => 'PagesController#about']);
Similarly a 'post' routing:
Route::post('/about-us', [ 'as' => 'about', 'uses' => 'PagesController#about']);
Remember to name the controller file as "PagesController". Its the proper naming convention.
"#about" is the method in the PagesController that will handle the logic.
For better understanding: Laravel Routing
Hope this is helpful.
this is my update function in usercontroller
public function update_user_credentials(UpdateUserRequest $request)
{
$user = User::find($request->user()->id);
if(!$user)
{
return response('User not found', 404);
}
try
{
$data=Input::all();
$user->fill($data);
var_dump($user);
exit;
$user->save();
}
catch(Exception $ex)
{
return response($ex->getMessage(),400);
echo Success::get('message');
}
return Redirect::back()->with('message','updated');
}
my UpdateUserRequest.php
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class UpdateUserRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'first_name' =>'required',
'last_name'=>'required',
'url'=>'url',
'password'=>'min:6|confirmed',
'password_confirmation'=>'min:6',
'email'=>'email|unique:users,email',
];
}
}
Each column has its own form. So updating email has its own form as does password.
When I'm updating without putting UpdateUserRequest inside my update controller, it works fine. But when I add that in for validations, nothing happens. I get a 302 error but I don't any messages.
I tried getting msgs with validator->messages but also got nothing.
Also if I put in
protected $redirect = '/'
I do get redirected. That means validation is working right?
here are my routes if it helps:
Route::get('/account/email',function(){
$user=Request::user();
$id = $user->id;
return Response::view('user.edit.email', compact('user'));
});
Route::patch('/account/update','UserController#update_user_info');
Update So I figured out how to display error messages and one of them was that 'first name and last name were required'. But now I get this error when I try updating
ErrorException in UserController.php line 93:
Missing argument 2 for App\Http\Controllers\UserController::update_user_credentials()
in UserController.php line 93
at HandleExceptions->handleError('2', 'Missing argument 2 for App\Http\Controllers\UserController::update_user_credentials()', '/Users/Jack/projects/makersBrand/laravel/app/Http/Controllers/UserController.php', '93', array('request' => object(UpdateUserRequest))) in UserController.php line 93
at UserController->update_user_credentials(object(UpdateUserRequest))
at call_user_func_array(array(object(UserController), 'update_user_credentials'), array(object(UpdateUserRequest))) in Controller.php line 256
at Controller->callAction('update_user_credentials', array(object(UpdateUserRequest))) in ControllerDispatcher.php line 164
at ControllerDispatcher->call(object(UserController), object(Route), 'update_user_credentials') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(UserController), object(Route), object(Request), 'update_user_credentials') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\UserController', 'update_user_credentials') in Route.php line 201
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 704
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
Does that mean '2' is being passed into validations?
I think your validation rule will not work. If you update your data your validation rule also check the same row which you are updating.What I am trying to say is that suppose you are trying to update the row having id 2 , then when you doing the validation your validation rule also check the row having id 2 for uniqueness for email.Suppose you didn't update your email , then while validating your validation rule will check all row and it will not find the email unique and it should display the error.
Your route must be
Route::post('/account/update/{id}','UserController#update_user_info');
While updating your rules must check all row except the row that you are updating.You can apply the same rule to create and to update the data like this
public function rules(){
return [
'first_name' =>'required',
'last_name'=>'required',
'url'=>'url',
'password'=>'min:6|confirmed',
'password_confirmation'=>'min:6',
// you need to change here
'email'=>'email|unique:users,email,'.$this->route()->getParameter('id').',database_id'
];
// database_id means your table primary key column i.e id
}
I think your update function must look something like this
public function update_user_credentials(Requests\UpdateUserRequest $request,$id){
// code
}
A fluent way to handle this in L5.3+ is to use the Rule facade
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
Note that you need to specify the validation rules as an array instead of using the | character to delimit the rules.
I'm pretty new to using Swagger. Since my project is with Laravel, I use Swaggerevel to document my API. During I tried to generate as follows,
./vendor/bin/swagger app/ -o storage/docs/api-docs.json
It shows that
[INFO] Required #SWG\Info() not found
get /api/resource.json
-----------------------
1 operations documented
-----------------------
Written to /home/admin/api/gevme-api/storage/docs/api-docs.json
When I tried to access, It localhost:8000/docs, It properly show json api which I generated. But when I tried to access localhost:8000/api-docs, the same error message show again.
ErrorException in Logger.php line 38:
Required #SWG\Info() not found
in Logger.php line 38
at HandleExceptions->handleError('1024', 'Required #SWG\Info() not found', '/home/admin/api/gevme-api/vendor/zircote/swagger-php/src/Logger.php', '38', array('entry' => 'Required #SWG\Info() not found', 'type' => '1024'))
at trigger_error('Required #SWG\Info() not found', '1024') in Logger.php line 38
at Logger->Swagger\{closure}('Required #SWG\Info() not found', '1024')
at call_user_func(object(Closure), 'Required #SWG\Info() not found', '1024') in Logger.php line 68
at Logger::notice('Required #SWG\Info() not found') in AbstractAnnotation.php line 365
at AbstractAnnotation->validate() in Analysis.php line 284
at Analysis->validate() in functions.php line 46
at Swagger\scan('/home/admin/api/gevme-api/modules/Api', array('exclude' => array('/home/admin/api/gevme-api/storage', '/home/admin/api/gevme-api/tests', '/home/admin/api/gevme-api/resources/views', '/home/admin/api/gevme-api/config', '/home/admin/api/gevme-api/vendor'))) in routes.php line 39
at SwaggervelServiceProvider->{closure}()
at call_user_func_array(object(Closure), array()) in Route.php line 155
at Route->runCallable(object(Request)) in Route.php line 130
at Route->run(object(Request)) in Router.php line 704
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in OAuthExceptionHandlerMiddleware.php line 36
at OAuthExceptionHandlerMiddleware->handle(object(Request), object(Closure))
at call_user_func_array(array(object(OAuthExceptionHandlerMiddleware), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
at require_once('/home/admin/api/gevme-api/public/index.php') in server.php line 21
The problem is - you're missing the #SWG\Info block.
It's a block which tells swagger some most common information about your API.
I usually put this in a separate controller which is rendering swagger JSON.
Here's an example:
/**
* #SWG\Swagger(
* schemes={"http","https"},
* host="api.host.com",
* basePath="/",
* #SWG\Info(
* version="1.0.0",
* title="This is my website cool API",
* description="Api description...",
* termsOfService="",
* #SWG\Contact(
* email="contact#mysite.com"
* ),
* #SWG\License(
* name="Private License",
* url="URL to the license"
* )
* ),
* #SWG\ExternalDocumentation(
* description="Find out more about my website",
* url="http..."
* )
* )
*/
class SwaggerController extends...
If this happens to you, and you have the Swagger definition in the controller etc but still aren't seeing the comments make sure you don't have the following settings in your opcache configuration:
opcache.save_comments=1
opcache.load_comments=1
The settings above remove and not-load the docblox needed for swagger to create the documentation.
For Laravel 5.6 I launched
composer require darkaonline/l5-swagger:5.6
and quietly installed version 5.6.0 (which was linked to zircote/swagger-php version TWO), and I got subj error having #OA\Info block in Controller even.
When I runned
composer require darkaonline/l5-swagger:5.6.9
and increased version of zircote/swagger-php to
composer require zircote/swagger-php:3.0.2
my problem was solved
It is appening something that is very strange:
I'm implementing in Laravel 5, the Iron message queue in order to differ the request, from long tasks executions.
Once I've pushed a message into the Iron's queue, it sends a POST request to a predefined route, in order to wake up the long running process (Push Queue approach).
I've this route file:
Route::post('queue/receive', function()
{
//start long task exec
return Queue::marshal();
});
/* garantisco il logout agli utenti*/
Route::get('auth/logout', 'Auth\AuthController#getLogout');
/* redirect degli utenti loggati */
Route::group(['middleware' => 'guest'], function()
{
Route::get('/', 'WelcomeController#index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
});
Route::group(['middleware' => 'auth'], function()
{
Route::get('home', 'HomeController#index');
});
The first route defined is the endpoint that IronMQ will call.
I know that token mismatch is a popular problem using the "VerifyCsrfToken" middleware (same as filter in L4).
The incredible thing is that I've disabled this middleware, but the problem persists.
This is my kernel's middlewares:
class Kernel extends HttpKernel {
/**
* The application's global HTTP middleware stack.
*
* #var array
*/
protected $middleware = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
//'LabelCreator\Http\Middleware\VerifyCsrfToken',
];
/**
* The application's route middleware.
*
* #var array
*/
protected $routeMiddleware = [
'auth' => 'LabelCreator\Http\Middleware\Authenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'LabelCreator\Http\Middleware\RedirectIfAuthenticated',
];
}
As you can see, "VerifyCsrfToken" is disabled but testing the post request from local env, now I'm getting this error:
DecryptException in Encrypter.php line 142:
Invalid data.
in Encrypter.php line 142
at Encrypter->getJsonPayload('') in Encrypter.php line 92
at Encrypter->decrypt('') in IronQueue.php line 214
at IronQueue->parseJobBody('') in IronQueue.php line 173
at IronQueue->marshalPushedJob() in IronQueue.php line 159
at IronQueue->marshal()
at call_user_func_array(array(object(IronQueue), 'marshal'), array()) in QueueManager.php line 223
at QueueManager->__call('marshal', array()) in Facade.php line 207
at QueueManager->marshal() in Facade.php line 207
at Facade::__callStatic('marshal', array()) in routes.php line 17
at Queue::marshal() in routes.php line 17
at RouteServiceProvider->{closure}()
at call_user_func_array(object(Closure), array()) in Route.php line 153
at Route->runCallable(object(Request)) in Route.php line 128
at Route->run(object(Request)) in Router.php line 691
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 693
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 660
at Router->dispatchToRoute(object(Request)) in Router.php line 618
at Router->dispatch(object(Request)) in Kernel.php line 210
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 111
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
I've tried to comment the others middlewares, but the problem persists.
What Iron is doing is a simple POST request to my endpoint, like webhooks do.
What is the problem?
I don't think is a session's problem, because Iron is a simple thirdy part service that call an endpoint, and if it remains a "guest" client for my app it is ok.
Can anyone help me?
thanks in advance
I've found the problem:
I was testing the endpoint in local without a POST request payload, so the decryption doesn't work properly, but sniffing which payload IronMQ sends, using RequestBin (very useful tool), I've added the same payload to local test request, and now it works.
Hope it helps :)