从服务容器解析对象
make
$fooBar = $this->app->make('HelpSpot\API');
如果调用不了$app变量,可以使用辅助函数app或者App Facedes:
$api = app('HelpSpot\API');
$api = \App::make('HelpSpot\API');
自动注入
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use App\Users\Repository as UserRepository;
class UserController extends Controller{
/**
* 用户仓库实例
*/
protected $users;
/**
* 创建一个控制器实例
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
/**
* 通过指定ID显示用户
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
}
php