Home >Backend Development >PHP Tutorial >Laravel 5框架学习之向视图传送数据(进阶篇)_php实例

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

WBOY
WBOYOriginal
2016-06-07 17:13:36801browse

我们不仅仅可以向视图传送一个数据,同样我们可以传送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。

Statement:
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