Laravel中的页眉页脚控制器 [英] Header Footer Controller in Laravel

查看:55
本文介绍了Laravel中的页眉页脚控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次在Laravel上工作

I am working on Laravel for the first time

我必须在页眉和页脚中创建一个前端菜单动态[类别列表将来自数据库].我必须为此使用哪个控制器. 此框架中可用的任何通用控制器,用于将数据发送到页眉和页脚.

i have to make a Front End Menu Dynamic in Header and Footer [ list of categories will come from database ]. which controller I have to use for this.? any common controller available in this framework to send data to header and footer.

当我在HomeController索引Action中收到数据时,它仅可用于主页.

When I receive the data in HomeController index Action its available for the home page only.

class HomeController {
public function index() 
    {
        $categories = Category::get();
        return view('home', compact('categories'));
    }
}

谢谢.

推荐答案

对于

视图编写器是在呈现视图时调用的回调或类方法.如果您希望每次渲染视图时都将其绑定到视图,则视图编辑器可以帮助您将逻辑组织到一个位置.

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.

您可以通过将视图数组作为第一个参数传递给composer方法,将视图作曲器一次附加到多个视图:

You may attach a view composer to multiple views at once by passing an array of views as the first argument to the composer method:

View::composer(['partials.header', 'partials.footer'], function ($view) {
    $view->with('categories', [1, 2, 3]); // bind data to view
});

现在,您只需在视图(刀片模板)中检索$categories.

Now you could simply retrieve $categories within your view (blade template).

提示:常见的做法是创建一个名为ComposerServiceProvider的新服务提供程序,并将上述作曲器逻辑放在boot()方法中.

Tip: Common practice is to create a new service provider called ComposerServiceProvider and place the abovementioned composer logic in the boot() method.

这篇关于Laravel中的页眉页脚控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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