Answer the question
In order to leave comments, you need to log in
Error when creating custom provider in Laravel 5?
I so understood that the functions can be written down in the provider.
Created by Provider:
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MapsServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
}
}
class Maps extends ServiceProvider
{
public static function location($address)
{
$obj = null;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=true_or_false®ion=ru');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
if ($output !== false)
{
if ($location = json_decode($output))
{
if ($location->status == 'OK')
$obj = $location->results[0]->geometry->location;
}
}
curl_close($ch);
return $obj;
}
}
'providers' => [
'App\Providers\MapsServiceProvider',
],
'coords' => Maps::location($address)
Answer the question
In order to leave comments, you need to log in
1. Why did you put two classes in here? Or is it just for brevity?
2. PHP informs you in human language: the App\Providers\Maps class contains one abstract method and must either be declared abstract too, or it must fully implement all the remaining methods.
In short, why are you also extending the Maps class from ServiceProvider? You don't need to extend it from there. Learn OOP)
In general, I didn’t understand the logic in your code here. If you just want to create a static function in some of your class - just create a regular class, you don't need to make any ServiceProviders. Service providers create in order to expand the logic of the framework.
It swears that something from the inherited class is not described in you.
Check out the ServiceProvider parent class and see related materials: www.quizful.net/interview/php/abstract-class-inter...
Service providers are needed not to write functionality, but to initialize various parts of your application. Make a regular class, place a thread somewhere in the namespace, and call from the controller / other classes in the usual way.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question