以Laravel的返回视图函数为例
1.从helpers.php辅助函数中调用
function view($view = null, $data = array(), $mergeData = array())
{
$factory = app('Illuminate\Contracts\View\Factory');
if (func_num_args() === 0)
{
return $factory;
}
return $factory->make($view, $data, $mergeData);
在helpers.php中,对IOC容器函数调用重新封装,更为简洁。
return view('home');
rerurn view()->make('home');
2.调用Faceds函数
return View::make('home');
Illuminate\Support\Facades\View
class View extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'view'; }
}
Illuminate\Foundation\Application
'view' => ['Illuminate\View\Factory', 'Illuminate\Contracts\View\Factory'],
3.直接从IOC容器中调用
注意从controller或者其派生类中无法直接访问$app,所以要通过扩展函数app()获得$app对象
function app($make = null)
{
if ( ! is_null($make))
{
return app()->make($make);
}
return Illuminate\Container\Container::getInstance();
}
return app()->make('view')->make('home');
或者
return app('view')->make('home');