drew schmaltz
drew schmaltz

Reputation: 1584

Laravel 4 IoC Container - Setting Up & Using Providers

I am trying to use http://geotools-php.org/ in Laravel 4 (L4).

I followed the instructions for downloading through Composer (and verified geotools existance in the vendors folder).

I added "app/providers/GeoTools.php" to my autoload classmap.

Here's the contents:

{
    "require": {
        "laravel/framework": "4.0.*",
        "toin0u/geotools": "@stable"
    },
    "autoload": {
        "classmap": [
            "app/commands",
            ... more laravel stuff
            "app/providers/GeoTools.php"
        ]
    }
    ... more laravel stuff

I created /providers/GeoTools.php file.

Here are the contents:

<?php

use Illuminate\Support\ServiceProvider;

class GeoToolsServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('Coordinate', function()
        {

            return new Geotools\Coordinate;

        });
    }

}

In my app.php providers array I added "GeoToolsServiceProvider".

Here are my questions:

  1. Obviously I have done something wrong, how should this be done.
  2. Once you have the above code correct how do you properly use it throughout the application?

Upvotes: 0

Views: 1458

Answers (3)

toin0u
toin0u

Reputation: 136

I'm the maintainer of this library.

It's not possible (for the moment Laravel Bêta 5) to bind the Geotools\Coordinate\Coordinate class like you wanted to do because the Laravel IoC Container doest not accept arguments without default values or types (i.e. interfaces / classes) which are automatically resolved by Reflection. The container will throw an BindingResolutionException exception.

The best way is to extend the Geotools class and add a method which will create Coordinate instances.

I'm waiting for the stable version of Laravel 4 before to make any BC breaks in Geotools.

I just made the Geotools package for Laravel 4. I hope it will help you!

Upvotes: 3

Blessing
Blessing

Reputation: 4888

You should be ables to access the instance by using the app facade

App::make('Coordinate')->$someGeotoolCoordinateFunction();

Upvotes: 0

Cody Covey
Cody Covey

Reputation: 1060

The service provider looks okay actually. The issues are probably in autoloading and without seeing how you added to the class map its hard to tell what is going on. As far as usage try the following,

class Foo {

   protected $coordinate;

   public function __construct(Coordinate $coordinate)
   {
       $this->coordinate = $coordinate
   }
}

Upvotes: 0

Related Questions