Laravel 4:将视图模型注入Controller

时间:2015-03-20 07:47:45

标签: php laravel-4 dependency-injection

我刚刚开始学习Laravel并开始了Laracast的基础知识。在此episode 13中,它展示了如何使用构造函数将User模型注入UserController

但是当我尝试使用相同的技术注入ViewInput模型时,我遇到了一些错误:

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模型一样工作。有人可以向我解释为什么ViewInput不起作用?如何解决这个问题?

UserController中:

注意:我评论了不起作用的线条并抛出错误,如$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)
    {
        //
    }


}

1 个答案:

答案 0 :(得分:2)

因为你不能将Laravel外墙注入你的控制器。 Laravel中的每个外观都有一些关于如果要注入它应该使用哪个类的注释。例如:

/**
 * @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