6

下面的代码说明了一切......

// 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;
    }
}
4

2 回答 2

7

您可以尝试将带有参数的类直接绑定到您的应用容器中,例如

<?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);
        });
    }
}

保持不变您的 SimpleGeo.php。你可以在你的 routes.php 中测试它

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

var_dump ($test);
于 2013-03-27T09:40:53.767 回答
3

您需要将测试数组传递给服务提供者内部的类

// 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'));
    });
}

你的控制器.php

Public Class YourController
{
    public function __construct()
    {
        $this->simpleGeo = App::make('SimpleGeo');
    }
}
于 2013-03-25T02:50:01.500 回答