L
L
lynnikvadim2015-09-06 06:39:52
Laravel
lynnikvadim, 2015-09-06 06:39:52

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&region=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;
    }
}

Registered in app.php
'providers' => [				
        'App\Providers\MapsServiceProvider',
  ],

When I write in the controller I get an 'coords' => Maps::location($address)
error:
Class App\Providers\Maps contains 1 abstract method and must therefore be abstract or implement the remaining methods (Illuminate\Support\ServiceProvider::register)
What could be the problem?
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Akhmadullin, 2015-09-06
@saggid

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.

K
Kirill Arutyunov, 2015-09-06
@arutyunov

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...

A
ajaxtelamonid, 2015-09-07
@ajaxtelamonid

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 question

Ask a Question

731 491 924 answers to any question