咱们在开发项目的时候,视图的布局每每都是拥有一个统一的模版的,咱们不可能在每张页面都去写上相同布局的代码,咱们能够创建一个layout.blade.php
的视图文件,将基础布局写在这里,而后让别的视图文件都来继承它。html
如今打开项目,先查看下route.php
和PagesController.php
文件中的内容:laravel
<?php Route::get('/', 'PagesController@home'); Route::get('about', 'PagesController@about');
<?php namespace App\Http\Controllers; class PagesController extends Controller { public function home() { $users = ['Zhoujiping', 'Kuker Chou']; return view('welcome', compact('users')); } public function about() { return view('about'); } }
在resources/views/
下创建layout.blade.php的文件,输入如下内容:布局
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>布局文件</title> </head> <body> @yield('content') </body> </html>
而后将welcome.blade.php
代码改为以下:spa
@extends('layout') @section('content') <h1>这里是welcome.balde.php的页面</h1> @stop
咱们让全部的视图经过@extends('布局文件的名称')
去继承布局文件,经过@section()
去覆写@yield
就能够了,看下效果code
ok, 这节就这样.htm