从laravel中间件发送数据以进行查看 [英] Sending data from laravel middleware to view

查看:175
本文介绍了从laravel中间件发送数据以进行查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Laravel构建的Web应用程序中,我试图基于数据库中的数据生成菜单.

In a web application I'm building with Laravel I'm trying to generate a menu based upon data in the database.

我能够创建中间件,并将其添加到kernel.php中,以便在每次请求时执行该中间件.在中间件中,我可以从模型中获取数据.

I'm able to create the middleware and I've added it to kernel.php so that it's executed upon every request. In the middware I can fetch the data from the model.

现在,尽管我必须采取的下一步是将数据发送到视图.我已经在谷歌上搜索了,但是没有找到如何执行此操作的示例.我对中间件走错了路吗?或有某种方法可以做到这一点?

Now the next step I though I had to take was sending the data to the view. I've googled around for it, but I did not find any sample how to do this. Am I going the wrong way with middleware? or is there some way to do this?

推荐答案

虽然可能不是最有效的方法,但您可以使用视图编辑器轻松完成此操作.在没有问题之前,我曾在类似的情况下使用过它.

You can use view composers to do this quite easily though it may not be the most efficient method. I've used it in similar circumstances before without issue.

在生成菜单后,只需将其放入中间件即可.

Just drop it in your middleware after you've generated your menu.

view()->composer('*', function ($view) use ($menu) {
    $view->with('menu', $menu);
});

这篇关于从laravel中间件发送数据以进行查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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