Laravel 5 多个视图共享数据的方法

 

我们都知道模板一般会用到继承,导航栏就是一个很好的例子,但是导航栏的数据如何共享,比如有个导航的文件叫在view/navigation.blade.php
为了简单一点,文件里只有设置了一个变量
1
{$cqh }}
现在的要求是每个页面都会用到这个变量,如何共享这个数据呢?
一般这样的操作我会在Laravel服务的boot方法里设置,下面介绍两种共享数据的方法

方法一:使用View:share方法,如CqhServiceProvider里

1
2
3
4
public function boot()
{
    View::share('cqh','chenqionghe');
}
这是使用外观模式,也可以直接使用view()方法,
1
view()->share('cqh''chenqionghe');

 

方法二:使用composer方法单独对这个navigation.blade.php进行传送数据,方法如下
1
2
3
4
5
6
public function boot()
{
    view()->composer('navigation'function ($view) {
        $view->with('cqh','chenqionghe');
    });
}
以上是使用闭包的形式来完成这个操作,其实这和路由一样,也可以用类来代替,Laravel里叫组件,方法如下
1
2
3
4
public function boot()
{
    View::composer('navigation''App\Http\ViewComposers\MyViewComposer');
}
上面这样就是注册了视图组件,并且在每次 navigation视图渲染的时候,MyViewComposer@compose 都将会被执行。
下面我们来看这个组件怎么定义
1
2
3
4
5
6
7
8
9
<?php namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class MyViewComposer
{
    public function compose(View $view)
    {
        $view->with('cqh','chenqionghe');
    }
}
在视图被渲染之前,视图组件的 compose 方法就会被调用,并且传入一个 Illuminate\Contracts\View\View 实例。你可以使用 with 方法来把数据绑定到 view。
注意:方法一和方法二其实是有区别的,方法一是对所有的视图都传递的cqh数据,而方法二只是对navigation视图传递了cqh数据
如果方法二要达到方法一的效果,可以这样
1
2
3
4
View::composer('*'function($view)
{
    //
});
这样就会对所有视图进行传递了
也可以针对多个视图指定MyViewComposer组件
1
View::composer(['navigation''footer'], 'App\Http\ViewComposers\MyViewComposer');

 

也可以使用 composers 方法来同时定义一群视图组件
1
2
3
4
5
View::composers([
    'App\Http\ViewComposers\AdminComposer' => ['admin.index''admin.profile'],
    'App\Http\ViewComposers\UserComposer' => 'user',
    'App\Http\ViewComposers\ProductComposer' => 'product'
]);

转载于:https://www.cnblogs.com/mouseleo/p/10312510.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278413.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HR面 - 十大经典提问

1、HR&#xff1a;你希望通过这份工作获得什么&#xff1f; 1&#xff09;、自杀式回答&#xff1a;我希望自己为之工作的企业能够重视质量&#xff0c;而且会给做得好的员工予以奖励。我希望通过这份工作锻炼自己&#xff0c;提升自己的能力&#xff0c;能让公司更加重视我。 …

谷歌云使用账号密码_如何使用Google密码检查

谷歌云使用账号密码Google has a tool designed to securely analyze your passwords against a database of ones that are known to be compromised and breached. Password Checkup is available as an extension or a web service. Here’s how to use it. Google提供了一种…

HTML特殊字符编码对照表

HTML特殊字符编码对照表 特殊符号命名实体十进制编码特殊符号命名实体十进制编码特殊符号命名实体十进制编码Α&Alpha;Β&Beta;Γ&Gamma;Δ&Delta;Ε&Epsilon;Ζ&Zeta;Η&Eta;Θ&Theta;Ι&Iota;Κ&Kappa;Λ&Lambda;Μ&Mu;Ν&a…

CentOS 7.0下使用yum安装MySQL

CentOS7默认数据库是mariadb,配置等用着不习惯,因此决定改成mysql,但是CentOS7的yum源中默认好像是没有mysql的。为了解决这个问题&#xff0c;我们要先下载mysql的repo源。1.下载mysql的repo源$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm2.安装my…

Jolicloud是一款适合上网本的漂亮新操作系统

Want to breathe new life into your netbook? Here’s a quick look at Jolicloud, a unique new Linux based OS that lets you use your netbook in a whole new way. 想为您的上网本注入新的活力吗&#xff1f; 快速浏览一下Jolicloud&#xff0c;这是一个独特的基于Linu…

Repeater片段

1.字段过长截取字符串 1.1 截取字符串类 可以直接substring 也可以<%# Utility.Common.GetShow( Eval("NewTitle").ToString(),20,true) %><td><%#fcwms.Common.GetContent.GetShow(Eval("com_address").ToString(), 19, true)%> </t…

谷歌浏览器的翻译功能在哪_如何在Google表格中使用AND和OR功能

谷歌浏览器的翻译功能在哪If you’ve ever wanted to check whether data from your Google Sheets spreadsheet meets certain criteria, you can use AND and OR. These logical functions give you TRUE and FALSE responses, which you can use to sort through your data.…

Reptile:requests + Xpath 爬取段子网的段子

2019/1/24 中午路飞学成 爬虫课程 实验及笔记。 Xpath是路飞爬虫课程中老师说的三种解析方式之一&#xff0c;前面是re正则表达式的解析方式&#xff0c;现在是xpath的解析方式&#xff0c;后面还有一个是bs4的解析方式。 re其实我理解的很困难&#xff0c;而且到现在都还不怎么…

Android 系统权限

Android 是一个权限分隔的操作系统&#xff0c;其中每个应用都有其独特的系统标识&#xff08;Linux 用户 ID 和组 ID&#xff09;。系统各部分也分隔为不同的标识。Linux 据此将不同的应用之间、应用与系统之间分隔开来 ##一、安全架构 Android 安全架构的中心设计点是&#x…

【转载】负数的二进制

https://jingyan.baidu.com/article/29697b9106eb52ab21de3c7a.html 将十进制的负数变成二进制数的过程&#xff1a; 1.写出绝对值的二进制码&#xff08;原码&#xff09; 2.取反&#xff08;反码&#xff09; 3.1,&#xff08;补码&#xff09; 同理&#xff0c;将二进制的负…

保存网络文章以供以后使用Instapaper阅读

Have you ever come across a bunch of great articles that you want to read online, but just don’t have the time? Today we take a look at an online service that allows you to read your articles later, either online, or on an iPhone, or eReader. 您是否曾经遇…

谷歌chrome xp_将非Google任务列表添加到Chrome

谷歌chrome xpMost people rely on a task list to help them remember what they need to do but not everyone wants one that is tied to a Google account. If you have been wanting an independent tasks list then join us as we look at the Tasks extension for Googl…

学习笔记 - MarkDown 语法

学习参考网址&#xff1a;https://www.appinn.com/markdown/index.html # **gitskill**## 标题 ># 这是 H1 >## 这是 H2 >###### 这是 H6## 区块引用 Blockquotes > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet, consectetuer adipisci…

[BZOJ4671]异或图

description BZOJ 定义两个结点数相同的图\(G1\)与图\(G2\)的异或为一个新的图\(G\), 其中如果\((u,v)\)在\(G1\)与\(G2\)中的出现次数之和为\(1\), 那么边\((u,v)\)在\(G\)中, 否则这条边不在\(G\)中. 现在给定\(s\)个结点数相同的图\(G1...s\),设\(S{G1,G2,...,Gs},\) 问\(S\…

我们生活在最好的时代

2019独角兽企业重金招聘Python工程师标准>>> 没规划的人生叫拼图&#xff0c;有规划的人生叫蓝图&#xff1b; 没目标的人生叫流浪&#xff0c;有目标的人生叫航行&#xff01; 我们生活在最好的时代&#xff1a;在认知和学习机会上&#xff0c;人人平等&#xff0c…

MapReduce详解和WordCount模拟

最早接触大数据&#xff0c;常萦绕耳边的一个词「MapReduce」。它到底是什么&#xff0c;能做什么&#xff0c;原理又是什么&#xff1f;且听下文讲解。 是什么 MapReduce 即是一个编程模型&#xff0c;又是一个计算框架&#xff0c;它充分采用了分治的思想&#xff0c;将数据处…

无法创建系统映像_如何创建USB驱动器的映像

无法创建系统映像You can back up your USB drive by creating a saved image. You can then take that saved image and clone multiple USB sticks. This guide shows you how to create an image of your USB drive using Windows 10. 您可以通过创建保存的图像来备份USB驱动…

UGUI事件之Drag拖拽事件

UI事件之Drag拖拽事件2.UGUI 事件命名空间   当我们需要使用 UGUI 中的事件的时候&#xff0c;需要在脚本内引入专有命名空间&#xff1a;   using UnityEngine.EventSystems;----------------------------------2.拖拽相关事件接口----------------------------------1.三…

java 通过cookie判断是否登陆

protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// 判断cookie是否有登录信息Cookie[] cookies req.getCookies();boolean isLogin false;for(Cookie c : cookies){if("loginInfo".equals(c.getNa…

使用高级管理控制台获得对Windows Home Server的扩展访问

Windows Home Server is easy to setup and use so anyone with basic computer knowledge can operate their own server. But what if you’re an advanced user and want more control over various administrative functions? The Advanced Admin Console Addin gives you…