Laravel 5.1 : Passing Data to View Composer

前端 未结 3 577
天命终不由人
天命终不由人 2021-02-06 15:20

I\'m using view composers in Laravel 5.1: nice. But how can I pass parameters to a view composer?

In my case I send week info (previous, current, next week, inc

3条回答
  •  孤街浪徒
    2021-02-06 15:57

    I think if you want to pass some data to the view composer from controller then you can use sessions. Set the data from the controller in the session and you can get data from session in the view composer. This did the trick for me without doing any complex stuff

    Controller

        public function index()
        {
           session(['key' => 'value']);
           return view('your_view');
        }
    

    View Composer

        public function compose(View $view)
        {
            $data = session('key');
            return $view->with('your_data',$data);
        }
    

提交回复
热议问题