I have a class which is responsible for making a request to a REST API which needs the name of the player and the platform on which it plays (I will simplify the code to get to the point).
class GetPlayerAPI
{
public function __construct(String $playerName, String $platform)
{
// Aqui construyo la url a la que hacer la peticion
}
public function getJsonFromApi()
{
// Aqui obtengo los datos mediante CURL
}
}
In the context where I am receiving $ playerName and $ platform of a form, I have registered the GetPlayerAPI class as a service in the following way (using request )
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind('GetPlayerAPI', function () {
return new GetPlayerAPI(request('playerName'), request('platform'));
});
}
And in the controller:
// Metodo llamado de un formulario
public function show()
{
$playerAPI = resolve('GetPlayerAPI');
}
And this is where I'm left with doubts because if I wanted to reuse the GetPlayerAPI class in another context where I did not receive the parameters using a form, I could not do it since the bind uses the request.
For example another controller method that is not called from a form:
// Controlador
public function noLlamadoDeFormulario()
{
$playerName = 'Juan';
$platForm = 'Play Station';
// No puedo usar la clase GetPlayerAPI
$playerAPI = resolve('GetPlayerAPI');
}
No doubt I must be misusing the Service Container because in this case it is not practical.
I believe that the advantage of the Service Container is to be able to use the classes in all the code without worrying about its instantiation.
Could you guide me?