6

The code below says it all...

// routes.php
App::make('SimpleGeo',array('test')); <- passing array('test')

// SimpleGeoServiceProvider.php
public function register()
{
    $this->app['SimpleGeo'] = $this->app->share(function($app)
    {
        return new SimpleGeo($what_goes_here);
    });
}

// SimpleGeo.php
class SimpleGeo 
{
    protected $_test;

    public function __construct($test) <- need array('test')
    {
        $this->_test = $test;
    }
    public function getTest()
    {
        return $this->_test;
    }
}
Phill Sparks
  • 20,000
  • 3
  • 33
  • 46
drew schmaltz
  • 1,584
  • 4
  • 19
  • 29
  • 1
    Hello @schmaltz have you reached an answer for your question? I'm having the same problem and looking for a solution as my application uses similar architecture like yours.. – Omranic Feb 13 '14 at 05:40

2 Answers2

7

You can try to bind the class with the parameters directly into your app container, like

<?php // This is your SimpleGeoServiceProvider.php

use Illuminate\Support\ServiceProvider;

Class SimpleGeoServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('SimpleGeo', function($app, $parameters)
        {
            return new SimpleGeo($parameters);
        });
    }
}

leaving untouched your SimpleGeo.php. You can test it in your routes.php

$test = App::make('SimpleGeo', array('test'));

var_dump ($test);
Antonio Frignani
  • 961
  • 1
  • 11
  • 23
  • If I do App::bind like yours I get App is not defined. If I keep it like mine but add the 2nd parameter $parameters I get Warning: Missing argument 2 – drew schmaltz Mar 27 '13 at 17:19
  • It's strange, for me is working normally. I've put my **SimpleGeo.php** and **SimpleGeoSeriveProvider.php** in __/libraries__ folder (and added the path loader in the composer.json file and then issuing a **composer dump-autoload** command) and added **'SimpleGeoServiceProvider'** to the providers array in __app/config/app.php__. Btw, is the error raised in SimpleGeoServiceProvider.php or in your routes.php? – Antonio Frignani Mar 27 '13 at 21:04
  • Well, I built mine with the "workbench" command so it resides in workbench/. It does throw the error in SimpleGeoServiceProvider. I'm going to double check and get back to you. – drew schmaltz Mar 27 '13 at 21:49
3

You need to pass your test array to the class inside of the service provider

// NOT in routes.php but when u need it like the controller
App::make('SimpleGeo'); // <- and don't pass array('test')

public function register()
{
    $this->app['SimpleGeo'] = $this->app->share(function($app)
    {
        return new SimpleGeo(array('test'));
    });
}

YourController.php

Public Class YourController
{
    public function __construct()
    {
        $this->simpleGeo = App::make('SimpleGeo');
    }
}
Dipendra Gurung
  • 5,720
  • 11
  • 39
  • 62
  • yeah, i noticed that worked. My value isn't static though and has to be passed somehow. For example: it's available in Route as Input. – drew schmaltz Mar 25 '13 at 04:15
  • sorry, just noticed this. Might be something like this u need http://stackoverflow.com/questions/15483542/laravel-4-way-to-inject-an-object-that-requires-configuration-into-a-controller – Juni Samos De Espinosa Apr 08 '13 at 00:13