php - Laravel 5 HttpNotFoundException -


when validate incoming request in laravel 5.x, if validation fails, symfony\component\httpkernel\exception\notfoundhttpexception raised instead of expected httpresponseexception.

if replace

throw new httpresponseexception($this->buildfailedvalidationresponse(             $request, $this->formatvalidationerrors($validator)         )); 

code validator trait other exception, works well...

routes

// user login (logout handled frontend) route::post('login', [     'as'   => 'login',     'uses' => 'auth\authcontroller@login' ]); 

controller

/**  * login user.  * @param  \illuminate\http\request $request  * @return \illuminate\contracts\routing\responsefactory|\symfony\component\httpfoundation\response  * @throws \app\exceptions\authenticationfailedexception  */ public function login(request $request) {     $this->validate($request, [         'username' => ['required'],         'password' => ['required'],     ]);      $credentials = $request->only('username', 'password');      if (!$this->auth->once($credentials)) {         // went wrong whilst attempting encode token         throw new authenticationfailedexception;     }      $token = $this->jwt->fromuser($this->auth->user(), [         'permissions' => $this->auth->user()->role,         'name'        => $this->auth->user()->name     ]);      return response(compact('token')); } 

exception

[2015-07-02 07:42:20] local.error: exception 'symfony\component\httpkernel\exception\notfoundhttpexception' in /var/www/public/project/vendor/laravel/framework/src/illuminate/routing/routecollection.php:143 stack trace: #0 /var/www/public/project/vendor/laravel/framework/src/illuminate/routing/router.php(746): illuminate\routing\routecollection->match(object(illuminate\http\request)) #1 /var/www/public/project/vendor/laravel/framework/src/illuminate/routing/router.php(655): illuminate\routing\router->findroute(object(illuminate\http\request)) #2 /var/www/public/project/vendor/laravel/framework/src/illuminate/routing/router.php(631): illuminate\routing\router->dispatchtoroute(object(illuminate\http\request)) #3 /var/www/public/project/vendor/laravel/framework/src/illuminate/foundation/http/kernel.php(229): illuminate\routing\router->dispatch(object(illuminate\http\request)) #4 [internal function]: illuminate\foundation\http\kernel->illuminate\foundation\http\{closure}(object(illuminate\http\request)) #5 /var/www/public/project/vendor/laravel/framework/src/illuminate/pipeline/pipeline.php(139): call_user_func(object(closure), object(illuminate\http\request)) #6 /var/www/public/project/vendor/laravel/framework/src/illuminate/view/middleware/shareerrorsfromsession.php(54): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #7 [internal function]: illuminate\view\middleware\shareerrorsfromsession->handle(object(illuminate\http\request), object(closure)) #8 /var/www/public/project/vendor/laravel/framework/src/illuminate/pipeline/pipeline.php(124): call_user_func_array(array, array) #9 /var/www/public/project/vendor/laravel/framework/src/illuminate/session/middleware/startsession.php(62): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #10 [internal function]: illuminate\session\middleware\startsession->handle(object(illuminate\http\request), object(closure)) #11 /var/www/public/project/vendor/laravel/framework/src/illuminate/pipeline/pipeline.php(124): call_user_func_array(array, array) #12 /var/www/public/project/vendor/laravel/framework/src/illuminate/foundation/http/middleware/checkformaintenancemode.php(42): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #13 [internal function]: illuminate\foundation\http\middleware\checkformaintenancemode->handle(object(illuminate\http\request), object(closure)) #14 /var/www/public/project/vendor/laravel/framework/src/illuminate/pipeline/pipeline.php(124): call_user_func_array(array, array) #15 /var/www/public/project/app/http/middleware/apisession.php(24): illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #16 [internal function]: app\http\middleware\apisession->handle(object(illuminate\http\request), object(closure)) #17 /var/www/public/project/vendor/laravel/framework/src/illuminate/pipeline/pipeline.php(124): call_user_func_array(array, array) #18 [internal function]: illuminate\pipeline\pipeline->illuminate\pipeline\{closure}(object(illuminate\http\request)) #19 /var/www/public/project/vendor/laravel/framework/src/illuminate/pipeline/pipeline.php(103): call_user_func(object(closure), object(illuminate\http\request)) #20 /var/www/public/project/vendor/laravel/framework/src/illuminate/foundation/http/kernel.php(118): illuminate\pipeline\pipeline->then(object(closure)) #21 /var/www/public/project/vendor/laravel/framework/src/illuminate/foundation/http/kernel.php(86): illuminate\foundation\http\kernel->sendrequestthroughrouter(object(illuminate\http\request)) #22 /var/www/public/project/public/index.php(54): illuminate\foundation\http\kernel->handle(object(illuminate\http\request)) #23 {main}   

someone have idea ? can't find on laracast forum or github issues

this issue may occur when laravel tries redirect a page/form not exist error message.

you error when post form postman or similar test utility.

if trying build api must return json object, modify formrequest so:

edit app\http\requests\requests.php

<?php  namespace app\http\requests;  use illuminate\foundation\http\formrequest; use illuminate\http\jsonresponse;  abstract class request extends formrequest {     /**      * proper failed validation response request.      *      * @param  array  $errors      * @return \symfony\component\httpfoundation\response      */      public function response(array $errors)      {          //this return json object error messages          return new jsonresponse($errors, 422);;      } } 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -