Created
June 9, 2014 09:15
-
-
Save silentworks/72537bbda91310d0d2ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::get('/tweet', ['as' => 'tweet', 'uses' => 'TweetController@index']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() | |
{ | |
} | |
} |
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
Inside boot():
Then typehint it within your constructor:
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!