Laravel 5框架学习之向视图传送数据(进阶篇)_php实例

WBOY
Release: 2016-06-07 17:13:36
Original
748 people have browsed it

我们不仅仅可以向视图传送一个数据,同样我们可以传送Array

复制代码 代码如下:

    public function about()
    {
        return view('pages.about')->with([
            'first' => 'Zhang',
            'last' => 'Jinglin'
        ]);
    }

About {{ $first }} {{ $last }}


一种简洁的方式是这样:

复制代码 代码如下:

    public function about()
    {
        $data = [];
        $data['first'] = 'Zhang';
        $data['last'] = 'Jinglin';
        return view('pages.about', $data);
    }

结果相同,更简单的是这样:

复制代码 代码如下:

    public function about()
    {
        $first = 'Zhang';
        $last = 'Jinglin';
        return view('pages.about', compact('first', 'last'));
    }

compact 把参数变成数组,extract相反。可以查看php手册了解 compact ,顺道了解一下 extract。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!