Answer the question
In order to leave comments, you need to log in
Some php questions?
Good afternoon. Several questions have accumulated, tell me how to do it right.
1) Laravel has a container service. It is recommended to inject the desired class into the constructor or, if you are outside the container, then use make:
That is, you should never create objects through new at all? Only through type-hint or make?
With injection, everything is clear, but I don’t understand what flexibility make gives, because in the case of a class replacement, we still have to go and change the class in all places.
There is still binding and you can inject an interface class and get an instance of its class, but it says right there:
$api = $this->app->make('HelpSpot\API');
There is no need to bind classes into the container if they do not depend on any interfaces. The container does not need to be instructed on how to build these objects, since it can automatically resolve these objects using reflection.
class Gibhub implements VCSInterface
{
public function getRepositories(string $author) array
{
$response = Http::get('api.github.com/...')->json();
if (!isset($response['key']) {
throw new Exception('Not found');
}
return $response;
}
}
It's better not to use query builders inside controllers.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question