I have a hard time to understand how I can do this best. I understand how the facade works as syntactic sugar to present a clear static code layout style while keeping the code loose-coupled and testable.
However I have the following problem. Whenever I have a controller the controller is dependent on loads of classes. Lets take the input class as an example. Every controller is an IoC container so actually this is good. Because if I would like to change the input class with another class. I just have to create a new class which implement the right interface. However this means that EVERY controller in my app is dependent on the same input class. Do I understand this right?
So I read this very nice article: http://www.nathandavison.com/posts/view/16/using-dependency-injection-and-ioc-in-laravel-4-controllers
In short it suggests to use dependency injection (DI) in your controllers. E.G.
class UsersController extends BaseController
{
protected $response;
protected $request;
protected $validator;
protected $hasher;
protected $user;
public function __construct(
Response $response,
\Illuminate\Http\Request $request,
\Illuminate\Validation\Factory $validator,
\Illuminate\Hashing\BcryptHasher $hasher,
User $user
) {
$this->response = $response;
$this->request = $request;
$this->validator = $validator;
$this->hasher = $hasher;
$this->user = $user;
}
public function getIndex()
{
return $this->response->make('Hello World!');
}
}
and
$app->bind('UsersController', function($app) {
$controller = new UsersController(
new Response,
$app->make('request'),
$app->make('validator'),
$app->make('hash'),
new User
);
return $controller;
});
This does make sense but it was not the philosophy of the framework because it bypasses the facade... I would like to hear opinions about this approach. Is this good practice or not?
I think you've been following the same tutorial as another guy whose question I just answered. Instead of repeating myself, please see my answer to his question.