dourao3960 2015-03-20 07:47
浏览 52
已采纳

Laravel 4:将视图模型注入Controller

I am just starting to learn Laravel and started with the basics on Laracast. In this episode 13 it shows howto inject the User model into the UserController with the use of the constructor.

But when I try to inject the View and Input model using the same technique I run into some errors like:

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()

When I inject the Redirect model it just works like the User model. Could somebody explain to me why View and Input don't work? And howto fix this?

UserController:

NOTE: I commented the lines out that didn't work and threw the errors, the lines like $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条回答 默认 最新

  • dt97868 2015-03-20 08:20
    关注

    Because you can not inject Laravel facades into your controller. Every facade in Laravel has some adnotation about what class you should use if you want to inject it. For example:

    /**
     * @see \Illuminate\View\Factory
     */
    class View extends Facade {
    
        /**
         * Get the registered name of the component.
         *
         * @return string
         */
        protected static function getFacadeAccessor() { return 'view'; }
    
    }
    

    Like you see there is an adnotation @see which let you know that if you want to inject it to controller you should use Illuminate\View\Factory.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀