通过Laravel 5.2中的控制器将中间件中的变量传递给视图 [英] Pass variable from middleware to view via controller in Laravel 5.2

查看:144
本文介绍了通过Laravel 5.2中的控制器将中间件中的变量传递给视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel 5.2中尝试执行以下操作.

Hi I am trying to do the following in Laravel 5.2.

摘要:通过控制器将一些变量从中间件传递到视图

Summary: Pass some variables from middleware to view, via controller

详细:当客户登录后,无论他们想要什么路线,我们都需要检查他们是否已经完成了所有设置步骤"(每个步骤都指向不同的路线,例如,一个可以是公司信息,另一个可以是公司信息).产品设置等).如果他们已完成所有设置步骤,则让他们继续选择的路线,否则我们将重定向到适当的设置步骤"路线,以尚未完成的任何路线为准.

Detailed: When a client is logged in, no matter what route they want, we need to check if they have completed all "setup steps" (each step refers to a different route, e.g. one could be company info, another could be product settings, etc). If they have completed all setup steps, then let them proceed to their chosen route, otherwise we redirect to the appropriate "setup steps" route, whichever one they haven't completed yet.

所有客户端控制器都运行相同的中间件,称为NonSuperAdmin.我想将设置步骤"的检查放到这个中间件中,并从那里适当地重定向.如果通过此中间件将客户端重定向到设置路由,则需要通过适当的设置步骤控制器方法将"incompleteSetupTasks"集合传递给相关视图.

All client controllers run the same middleware, called NonSuperAdmin. I would like to put the checking of "setup steps" in this middleware, and redirect from there as appropriate. If client is redirected to a setup route by this middleware, we need the "incompleteSetupTasks" collection to be passed on to the relevant view, via the appropriate setup steps controller method.

这可能吗?非常感谢您的帮助.

Is this possible? Any help is much appreciated.

推荐答案

在中间件中使用会话处理程序

In the middleware use session handler

if($condition === true){
  $data = [ //your content ];
  Session::flash('your_key', $data);
}
next($request);

此数据也将在您的控制器和视图中可用

This data will also be available in your controller and in view

这是您可以在控制器中访问数据的方式

This is how you can access data in controller

public function  yourControllerAction($request)
{
   $somevariable = Session::get('your_key');
   $viewdata = [
      'content' => $somevariable
   ]
   return view('yourview', $viewdata); 
}

或直接在视图中访问会话数据

Or directly access the session data in view

//inblade
<p>
  Your html content
  @foreach(Session::get('your_key' as $data)
     //your stuff
  @endif
</p>

这篇关于通过Laravel 5.2中的控制器将中间件中的变量传递给视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆