Skip to content

Instantly share code, notes, and snippets.

@silentworks
Created June 9, 2014 09:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silentworks/72537bbda91310d0d2ba to your computer and use it in GitHub Desktop.
Save silentworks/72537bbda91310d0d2ba to your computer and use it in GitHub Desktop.
<?php
Route::get('/tweet', ['as' => 'tweet', 'uses' => 'TweetController@index']);
<?php
class TweetController extends \BaseController
{
protected $tweet;
public function __construct($tweet)
{
/* How do I access $app['tweet'] in this controller?
* without using App::make
*
* I want to inject through the constructor and still be able to use route the normal way
*/
$this->tweet = $tweet;
}
public function index ()
{
$this->tweet->send('Here is my message');
}
}
<?php
class TweetServiceProvider extends ServiceProvider
{
public function register()
{
$app = $this->app;
$app['tweet'] = $app->share(function ($app) {
$storage_provider = null;
$request_provider = null;
$config = array(
'key' => $app['config']['tweet.key'],
'secret' => $app['config']['tweet.secret'],
'token' => isset($app['config']['tweet.token']) ? $app['config']['tweet.token'] : null,
'token_secret' => isset($app['config']['tweet.token_secret']) ? $app['config']['tweet.token_secret'] : null,
'auth_method' => isset($app['config']['tweet.auth_method']) ? $app['config']['tweet.auth_method'] : null,
);
return new Tweet($config);
});
}
public function boot()
{
}
}
@acairns
Copy link

acairns commented Jun 23, 2014

Inside boot():

$this->app->bind('TweetInterface', 'Tweet');

Then typehint it within your constructor:

public function __constructor(TweetInterface $tweet){}

When it's resolved from the IoC, it'll find the binding of the interface and inject Tweet as the dependency.
Bind also accepts a closure, so you could return App::make('Tweet') within the binding.

Create and implement the interface on the classes too!

@silentworks
Copy link
Author

Thanks @acairns I think I should have been more clear with my question, this is a third party package and I wouldn't want to hack the package. Would App::alias('tweet', 'Tweet') work? I think there should be some docs around creating third party package creation.

Because I think this is how it should have been done:

<?php

class TweetServiceProvider extends ServiceProvider
{
    public function register()
    {
        $app = $this->app;

        $app['Tweet'] = $app->share(function ($app) {

            $storage_provider = null;
            $request_provider = null;

            $config = array(
                'key'        => $app['config']['tweet.key'],
                'secret'     => $app['config']['tweet.secret'],
                'token'        => isset($app['config']['tweet.token']) ? $app['config']['tweet.token'] : null,
                'token_secret' => isset($app['config']['tweet.token_secret']) ? $app['config']['tweet.token_secret'] : null,
                'auth_method'  => isset($app['config']['tweet.auth_method']) ? $app['config']['tweet.auth_method'] : null,
            );

            return new Tweet($config);
        });

        // This should always be the way, this way I can get via Alias or by Full Class Name to use Class Automatic Resolution
        $app->alias('Tweet', 'tweet');
    }

    public function boot()
    {
    }
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment