Home  >  Article  >  Backend Development  >  Laravel 5框架学习之向视图传送数据(进阶篇)_PHP

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

WBOY
WBOYOriginal
2016-05-30 08:48:04782browse

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