{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/14982","repository_url":"https://api.github.com/repos/laravel/framework","labels_url":"https://api.github.com/repos/laravel/framework/issues/14982/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/14982/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/14982/events","html_url":"https://github.com/laravel/framework/issues/14982","id":172829062,"number":14982,"title":"Aborting in a controller constructor causes the controller to be constructed again.","user":{"login":"malhal","id":916912,"avatar_url":"https://avatars.githubusercontent.com/u/916912?v=3","gravatar_id":"","url":"https://api.github.com/users/malhal","html_url":"https://github.com/malhal","followers_url":"https://api.github.com/users/malhal/followers","following_url":"https://api.github.com/users/malhal/following{/other_user}","gists_url":"https://api.github.com/users/malhal/gists{/gist_id}","starred_url":"https://api.github.com/users/malhal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/malhal/subscriptions","organizations_url":"https://api.github.com/users/malhal/orgs","repos_url":"https://api.github.com/users/malhal/repos","events_url":"https://api.github.com/users/malhal/events{/privacy}","received_events_url":"https://api.github.com/users/malhal/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-08-23T23:12:06Z","updated_at":"2016-08-25T11:14:44Z","closed_at":null,"body":"Put an abort in a controller constructor and you will find it will be called again, this causes the exception to be rendered twice. Below is an example stack trace from the second time the controller was constructed. It would appear to me that during the terminate process it attempts to gather the middleware which includes controller middleware so to get that it does need to construct the controller again. \r\n\r\n```\r\nconstruct{\"error\":\"HttpException\",\"reason\":\"test\"}construct\r\n#0  App\\Http\\Controllers\\RestController->__construct()\r\n#1  ReflectionClass->newInstanceArgs(Array ()) called at [/Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Container/Container.php:779]\r\n#2  Illuminate\\Container\\Container->build(App\\Http\\Controllers\\RestController, Array ()) called at [/Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Container/Container.php:629]\r\n#3  Illuminate\\Container\\Container->make(App\\Http\\Controllers\\RestController, Array ()) called at [/Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:699]\r\n#4  Illuminate\\Foundation\\Application->make(App\\Http\\Controllers\\RestController) called at [/Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Routing/Route.php:203]\r\n#5  Illuminate\\Routing\\Route->getController() called at [/Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Routing/Route.php:316]\r\n#6  Illuminate\\Routing\\Route->controllerMiddleware() called at [/Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Routing/Route.php:278]\r\n#7  Illuminate\\Routing\\Route->gatherMiddleware() called at [/Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Routing/Router.php:655]\r\n#8  Illuminate\\Routing\\Router->gatherRouteMiddleware(Illuminate\\Routing\\Route Object called at [/Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:188]\r\n#9  Illuminate\\Foundation\\Http\\Kernel->gatherRouteMiddleware(Illuminate\\Http\\Request Object called at [/Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:162]\r\n#10 Illuminate\\Foundation\\Http\\Kernel->terminate(Illuminate\\Http\\Request Object  Symfony\\Component\\HttpKernel\\Exception\\HttpException: test in /Users/mh/Development/Laravel/wfff-passwords/vendor/laravel/framework/src/Illuminate/Foundation/Application.php on line <i>907</i></th></tr>\r\ncalled at [/Users/mh/Development/Laravel/wfff-passwords/public/index.php:58]\r\n```\r\n\r\nI discovered this when trying to make a validation result which applied to every POST method like this:\r\n\r\n  ```\r\n  public function __construct()\r\n    {\r\n        echo 'construct';\r\n        if(\\Illuminate\\Support\\Facades\\Request::method() == 'POST'){\r\n            if(Input::has('id')){\r\n                throw new BadRequestHttpException('id must not be in the body');\r\n            }\r\n        }\r\n```\r\n\r\nhowever the actual example that generated the trace above was this:\r\n\r\n    static $x = -1;\r\n    public function __construct()\r\n    {\r\n        echo 'construct';\r\n        self::$x = self::$x +1;\r\n        if( self::$x){\r\n            debug_print_backtrace();\r\n            die();\r\n        }\r\n        abort(500, 'test');\r\n\r\n    }\r\n\r\nI could investigate this more if you like, or I could just workaround it and remember never to abort in a controller constructor."},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/242352374","html_url":"https://github.com/laravel/framework/issues/14982#issuecomment-242352374","issue_url":"https://api.github.com/repos/laravel/framework/issues/14982","id":242352374,"user":{"login":"GrahamCampbell","id":2829600,"avatar_url":"https://avatars.githubusercontent.com/u/2829600?v=3","gravatar_id":"","url":"https://api.github.com/users/GrahamCampbell","html_url":"https://github.com/GrahamCampbell","followers_url":"https://api.github.com/users/GrahamCampbell/followers","following_url":"https://api.github.com/users/GrahamCampbell/following{/other_user}","gists_url":"https://api.github.com/users/GrahamCampbell/gists{/gist_id}","starred_url":"https://api.github.com/users/GrahamCampbell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GrahamCampbell/subscriptions","organizations_url":"https://api.github.com/users/GrahamCampbell/orgs","repos_url":"https://api.github.com/users/GrahamCampbell/repos","events_url":"https://api.github.com/users/GrahamCampbell/events{/privacy}","received_events_url":"https://api.github.com/users/GrahamCampbell/received_events","type":"User","site_admin":false},"created_at":"2016-08-25T11:14:44Z","updated_at":"2016-08-25T11:14:44Z","body":"You should never use constructors in controllers to do things like this. For example, we instantiate them when you run the route:list command. However, there does remain a bug here where by if an exception was thrown for a genuine reason, we get double errors."}}
{"actor":{"display_login":"GrahamCampbell"}}