Home > Backend Development > PHP Tutorial > 数据共享给所有视图laravel

数据共享给所有视图laravel

WBOY
Release: 2016-06-06 20:34:01
Original
1002 people have browsed it

<code>ProductController.php 
</code>
Copy after login
Copy after login

里面的 public function index() 的数据
如何 给 HomeController.php 里面的 public function index()

ProductController.php 里面

<code>$types = TypeSml::all();
$type = Type::all();
view()->share('types', $types);
view()->share('type', $type)
</code>
Copy after login
Copy after login
<code>HomeController.php

怎么调 ProductController.php 里面的 数据呢
</code>
Copy after login
Copy after login

回复内容:

<code>ProductController.php 
</code>
Copy after login
Copy after login

里面的 public function index() 的数据
如何 给 HomeController.php 里面的 public function index()

ProductController.php 里面

<code>$types = TypeSml::all();
$type = Type::all();
view()->share('types', $types);
view()->share('type', $type)
</code>
Copy after login
Copy after login
<code>HomeController.php

怎么调 ProductController.php 里面的 数据呢
</code>
Copy after login
Copy after login

view()->share是将数据共享给所有视图。

每次都只会请求单个控制器啊,而且数据最后也还是由控制器交给了视图啊,为什么你会在控制器直接共享数据呢?
如果你是为了代码复用,有2个方法:
1.定义基类控制器,其他控制器继承基类控制器;
2.将业务逻辑封装为命令,哪个控制器需要就在那里调用。

我也是不明白为什么view()会有一个share函数

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template