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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?