Laravel 4: 将视图模型注入控制器
Laravel 4: Inject view model into Controller
我刚刚开始学习 Laravel 并从 Laracast. In this episode 13 的基础知识开始,它展示了如何使用构造函数。
但是当我尝试使用相同的技术注入 View
和 Input
模型时,我 运行 出现了一些错误,例如:
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Call to undefined method Illuminate\Support\Facades\View::make()
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Call to undefined method Illuminate\Support\Facades\Input::all()
当我注入 Redirect
模型时,它就像 User
模型一样工作。有人可以向我解释为什么 View
和 Input
不起作用吗?以及如何解决这个问题?
用户控制器:
注意:我将无效的行注释掉并抛出错误,如 $this->view->make();
class UserController extends \BaseController {
protected $user, $redirect, $view, $input;
public function __construct(User $user, Redirect $redirect, View $view, Input $input)
{
$this->user = $user;
$this->redirect = $redirect;
$this->view = $view;
$this->input = $input;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$users = $this->user->all();
return View::make('users.index', ['users' => $users]);
// TODO: Why does below not work?
// return $this->view->make('users.index', ['users' => $users]);
}
/**
* Show the form for creating a net
* @return Response
*/
public function create()
{
return View::make('users.create');
// TODO: Why does below not work?
// return $this->view->make('users.create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$input = Input::all();
// TODO: Why does below not work?
// $input = $this->input->all();
if ( ! $this->user->fill($input)->isValid() )
{
return $this->redirect->back()->withInput()->withErrors($this->user->errors);
}
$this->user->save();
return $this->redirect->route('users.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$user = $this->user->find($id);
return View::make('users.show', ['user' => $user]);
// TODO: Why does below not work?
// return $this->view->make('users.show', ['user' => $user]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
因为您无法将 Laravel 外观注入到您的控制器中。 Laravel 中的每个门面都有一些关于 class 如果你想注入它应该使用什么的注释。例如:
/**
* @see \Illuminate\View\Factory
*/
class View extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'view'; }
}
就像你看到的那样,有一个注释 @see
让你知道如果你想将它注入控制器你应该使用 Illuminate\View\Factory
.
我刚刚开始学习 Laravel 并从 Laracast. In this episode 13 的基础知识开始,它展示了如何使用构造函数。
但是当我尝试使用相同的技术注入 View
和 Input
模型时,我 运行 出现了一些错误,例如:
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Call to undefined method Illuminate\Support\Facades\View::make()
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Call to undefined method Illuminate\Support\Facades\Input::all()
当我注入 Redirect
模型时,它就像 User
模型一样工作。有人可以向我解释为什么 View
和 Input
不起作用吗?以及如何解决这个问题?
用户控制器:
注意:我将无效的行注释掉并抛出错误,如 $this->view->make();
class UserController extends \BaseController {
protected $user, $redirect, $view, $input;
public function __construct(User $user, Redirect $redirect, View $view, Input $input)
{
$this->user = $user;
$this->redirect = $redirect;
$this->view = $view;
$this->input = $input;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$users = $this->user->all();
return View::make('users.index', ['users' => $users]);
// TODO: Why does below not work?
// return $this->view->make('users.index', ['users' => $users]);
}
/**
* Show the form for creating a net
* @return Response
*/
public function create()
{
return View::make('users.create');
// TODO: Why does below not work?
// return $this->view->make('users.create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$input = Input::all();
// TODO: Why does below not work?
// $input = $this->input->all();
if ( ! $this->user->fill($input)->isValid() )
{
return $this->redirect->back()->withInput()->withErrors($this->user->errors);
}
$this->user->save();
return $this->redirect->route('users.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$user = $this->user->find($id);
return View::make('users.show', ['user' => $user]);
// TODO: Why does below not work?
// return $this->view->make('users.show', ['user' => $user]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
因为您无法将 Laravel 外观注入到您的控制器中。 Laravel 中的每个门面都有一些关于 class 如果你想注入它应该使用什么的注释。例如:
/**
* @see \Illuminate\View\Factory
*/
class View extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'view'; }
}
就像你看到的那样,有一个注释 @see
让你知道如果你想将它注入控制器你应该使用 Illuminate\View\Factory
.