{"action":"created","issue":{"url":"https://api.github.com/repos/slimphp/Slim/issues/977","labels_url":"https://api.github.com/repos/slimphp/Slim/issues/977/labels{/name}","comments_url":"https://api.github.com/repos/slimphp/Slim/issues/977/comments","events_url":"https://api.github.com/repos/slimphp/Slim/issues/977/events","html_url":"https://github.com/slimphp/Slim/issues/977","id":53637031,"number":977,"title":"Class Controllers, add option for dependency injection using __construct()","user":{"login":"connerbw","id":812192,"avatar_url":"https://avatars.githubusercontent.com/u/812192?v=3","gravatar_id":"","url":"https://api.github.com/users/connerbw","html_url":"https://github.com/connerbw","followers_url":"https://api.github.com/users/connerbw/followers","following_url":"https://api.github.com/users/connerbw/following{/other_user}","gists_url":"https://api.github.com/users/connerbw/gists{/gist_id}","starred_url":"https://api.github.com/users/connerbw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/connerbw/subscriptions","organizations_url":"https://api.github.com/users/connerbw/orgs","repos_url":"https://api.github.com/users/connerbw/repos","events_url":"https://api.github.com/users/connerbw/events{/privacy}","received_events_url":"https://api.github.com/users/connerbw/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":10,"created_at":"2015-01-07T14:26:33Z","updated_at":"2015-03-13T16:13:09Z","closed_at":"2015-02-25T17:47:21Z","body":"Hi.\r\n\r\nClass Controllers feature as described in 2.4.0 release notes:\r\n\r\n_$app->get('/hello/:name', '\\Greeting:sayHello'); will instantiate class \\Greeting and pass the value \"Josh\" into its sayHello method._\r\n\r\nI would like to see this improved so that Dependency Injection using `__construct` could be satisfied.\r\n\r\nExample:\r\n\r\n```\r\n$app->get('/hello/:name', '\\Greeting:sayHello', [$appInterface, $cachePoolInterface, $adapterInterface]); \r\n```\r\n\r\nThird param is an array that gets passed into to `\\Greeting::__construct($app, $cache, $adapter)`\r\n\r\nThank you for your consideration."},"comment":{"url":"https://api.github.com/repos/slimphp/Slim/issues/comments/79087574","html_url":"https://github.com/slimphp/Slim/issues/977#issuecomment-79087574","issue_url":"https://api.github.com/repos/slimphp/Slim/issues/977","id":79087574,"user":{"login":"eko3alpha","id":1705908,"avatar_url":"https://avatars.githubusercontent.com/u/1705908?v=3","gravatar_id":"","url":"https://api.github.com/users/eko3alpha","html_url":"https://github.com/eko3alpha","followers_url":"https://api.github.com/users/eko3alpha/followers","following_url":"https://api.github.com/users/eko3alpha/following{/other_user}","gists_url":"https://api.github.com/users/eko3alpha/gists{/gist_id}","starred_url":"https://api.github.com/users/eko3alpha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eko3alpha/subscriptions","organizations_url":"https://api.github.com/users/eko3alpha/orgs","repos_url":"https://api.github.com/users/eko3alpha/repos","events_url":"https://api.github.com/users/eko3alpha/events{/privacy}","received_events_url":"https://api.github.com/users/eko3alpha/received_events","type":"User","site_admin":false},"created_at":"2015-03-13T16:13:09Z","updated_at":"2015-03-13T16:13:09Z","body":"I would also like to see this implemented.\r\n\r\nI am using class based routing and not able to inject dependencies let alone an IOC container.  My current solution is to create a BaseController that will provide the dependencies needed in the controllers.\r\n\r\nBaseController.php\r\n\r\n    class BaseController \r\n    {\r\n        protected $dependency1;\r\n        protected $dependency2;\r\n\r\n        public function __construct()\r\n        {\r\n               $this->dependency1 = new Dependency1;\r\n               $this->dependency1->initConfig(Config::get('some_config1'););\r\n\r\n               $this->dependency2 = new Dependency2;\r\n               $this->dependency2->initConfig(Config::get('some_config2'););\r\n        }\r\n    }\r\n\r\nMyController.php\r\n\r\n    class MyController extends BaseController\r\n    {\r\n        public function myMethod($param1)\r\n        {\r\n            $result = $this->dependency1->doSomething($param1);\r\n        }\r\n    }\r\n}\r\n\r\nroutes.php\r\n\r\n    $app->get('/get-r-dun/:param1/', 'MyController:myMethod');\r\n\r\n\r\nBaseController instantiates my dependencies.  Unfortunately I have no way to access my config values unless I use a registry pattern. \r\n\r\nConfig::get('db_host');\r\n\r\nThe other solution which I don't like is to make the BaseControllers constructor mount $app.\r\n\r\nBaseController\r\n\r\n    class BaseController \r\n    {\r\n        protected $app;\r\n        public function __construct()\r\n        {\r\n              global $app;  // I HATE this idea  >:(\r\n               $this->app = $app;\r\n        }\r\n    }\r\n\r\nThis is my least favorite of the 2.  Id love to be able to inject dependencies, or heck even an IOC.  Any news on this?"}}