
(1)、首先在app\Http\routes.php中定义路由;
1
2
3
|
Route::get( 'view' , 'ViewController@view' );
Route::get( 'article' , 'ViewController@article' );
Route::get( 'layout' , 'ViewController@layout' );
|
(2)、然后在Http\Controllers\ViewController.php中写入方法;
1
2
3
4
5
6
7
8
9
|
public function view(){
return view( 'index' );
}
public function article(){
return view( 'article' );
}
public function layout(){
return view( 'layout' );
}
|
(3)、然后在新建不同的视图文件,路径为:resources\views
1
2
3
|
index.blade.php
article.blade.php
layout.blade.php
|
重点:
1、使用include的方式:
一、在views下建立common目录文件,用于存放公共文件;
二、将公共内容放入common下,如在common建立了一个header.blade.php;
三、在视图中引入公共文件:
1
2
|
@ include ( 'common.header' )
|
另外,如果在header公共区域中有不同的数据,那么可以使用以下方式来传递数据:
1
2
3
4
|
@ include ( 'common.header' ,[ 'page' => '详细页面' ])
{{ $page }}--公共部分
|
那么,以上会输出:详细页面–公共部分
即传递成功
2、使用子视图的方式来引入,并且拥有相互传递数据的功能:
一、在views下建立layouts目录,其下放主视图。views下的则为子视图。
二、在layouts下建立home.blade.php主视图文件。可以供子视图调用。
三、在views目录下的layout.blade.php中引入主视图文件:采用继承的方式:
home主视图里:
1
2
3
4
5
6
7
|
<div class = "container" >
<!-- @yield( 'content' ) -->
yield是一个标识,标识是不一样的变量数据
@section( 'content' )
<b>我是主模板里的内容</b>
@show
|
子视图里:
//继承使用主视图
@extends('layouts.home')
//section可以获取主模板的内容
@section('content')
<!--@parent--> //parent意为:子模板可以获取主模板里的内容
我是layout的替换内容123
@endsection