Laravel - 视图
Laravel - 视图和 Blade 模板
Section titled “Laravel - 视图和 Blade 模板”在模型-视图-控制器(MVC)架构模式中,“视图”负责向用户呈现数据。在 Laravel 中,视图将你应用的显示逻辑与业务逻辑分离开来。视图通常是包含嵌入代码(使用 Blade,Laravel 的模板引擎)的 HTML 文件,存储在 resources/views 目录中。resources/views 目录下的子目录可用于进一步组织视图(例如,resources/views/posts/index.blade.php)。
基础视图示例
Section titled “基础视图示例”步骤 1: 创建一个简单的视图文件。将以下内容保存到 resources/views/greeting.blade.php (注意 .blade.php 扩展名,它启用了 Blade 模板):
<html> <body> <h1>Hello, World from Blade!</h1> </body></html>步骤 2: 在 routes/web.php 文件中创建一个路由来显示此视图:
routes/web.php
use Illuminate\Support\Facades\Route;
Route::get('/greet', function () { return view('greeting'); // 'greeting' 指的是 greeting.blade.php});步骤 3: 启动你的 Laravel 开发服务器 (php artisan serve) 并在浏览器中访问以下 URL:
步骤 4: 你应该会看到文本 “Hello, World from Blade!” 显示在一个 H1 标签中。
向视图传递数据
Section titled “向视图传递数据”通常,你需要将数据从控制器或路由逻辑传递到视图。你可以通过将一个数据数组作为第二个参数传递给 view 辅助函数来实现这一点。数组的键将成为在视图中可用的变量。
示例: 向视图传递姓名
Section titled “示例: 向视图传递姓名”步骤 1: 修改或创建一个视图文件,保存到 resources/views/user/profile.blade.php:
<html> <body> <h1>Hello, {{ $userName }}!</h1> <p>Welcome to your profile page.</p> </body></html>注意使用了 Blade 的输出语法 {{ $userName }}。这会显示 $userName 变量的值,并且也会自动转义 (escape) 以防止 XSS 攻击。
步骤 2: 在 routes/web.php 中添加一个路由,向此视图传递数据:
routes/web.php
Route::get('/profile/{name}', function ($name) { return view('user.profile', ['userName' => $name]); // 或者,使用 with() 方法: // return view('user.profile')->with('userName', $name);});步骤 3: 在浏览器中访问类似 http://localhost:8000/profile/Alice 的 URL。
步骤 4: 输出将是: “Hello, Alice!” 紧接着欢迎消息。为 userName 传递的值(本例中是 Alice)被动态地插入到视图中。
与所有视图共享数据
Section titled “与所有视图共享数据”有时,你可能需要使某一部分数据对应用渲染的所有视图都可用。Laravel 提供了 View::share() 方法来实现此目的。调用此方法的常用位置是在服务提供者 (service provider) 的 boot 方法中,例如 App\Providers\AppServiceProvider.php。
示例: 共享应用名称
Section titled “示例: 共享应用名称”步骤 1: 打开 app/Providers/AppServiceProvider.php。如果文件不存在,你可以创建它。导入 View Facade 并在 boot 方法中使用 View::share():
app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider{ /** * 注册任何应用服务。 * * @return void */ public function register() { // }
/** * 引导(Bootstrap)任何应用服务。 * * @return void */ public function boot() { View::share('appName', 'My Awesome Laravel App'); }}步骤 2: 现在,$appName 变量将在所有 Blade 视图中可用。例如,你可以在 resources/views/greeting.blade.php 中使用它:
<html> <body> <h1>Welcome to {{ $appName }}!</h1> <p>Hello, World from Blade!</p> </body></html>步骤 3: 如果你访问 http://localhost:8000/greet,输出现在将显示 “Welcome to My Awesome Laravel App!”,这表明共享数据是可访问的。
Blade 模板引擎
Section titled “Blade 模板引擎”Blade 是 Laravel 简单而强大的模板引擎。与其他 PHP 模板引擎不同,Blade 不限制你在视图中使用纯 PHP 代码。事实上,所有 Blade 视图都会被编译成纯 PHP 代码并缓存,直到它们被修改,这意味着 Blade 不会给你的应用增加任何开销。Blade 文件使用 .blade.php 文件扩展名。
关键 Blade 特性:
- 模板继承 (Template Inheritance): 定义一个主布局 (master layout),并在子视图 (child views) 中使用
@extends、@section和@yield等指令来扩展它。 - 控制结构 (Control Structures): Blade 为常见的 PHP 控制结构提供了方便的快捷方式,例如
@if、@foreach、@for、@while等。 - 显示数据 (Displaying Data): 使用
{{ $variable }}显示数据(默认为转义)。使用{!! $variable !!}显示原始、未转义的数据(谨慎使用)。 - 组件和插槽 (Components & Slots): 构建可重用 UI 元素的强大方式。
- 指令 (Directives): Blade 允许你使用
@directive编写自定义指令。
Blade 布局示例: 模板继承
Section titled “Blade 布局示例: 模板继承”步骤 1: 在 resources/views/layouts/app.blade.php 创建一个主布局文件:
<html><head> <title>@yield('title', 'My Application')</title> <link rel="stylesheet" href="{{ asset('css/app.css') }}"> <!-- 示例资源链接 --></head><body> <header> <h1>@yield('header_title', 'Welcome')</h1> </header>
@section('sidebar') <div class="sidebar"> <p>This is the master sidebar.</p> </div> @show
<div class="container"> @yield('content') </div>
<footer> <p>© {{ date('Y') }} My Application</p> </footer> <script src="{{ asset('js/app.js') }}"></script> <!-- 示例资源链接 --></body></html>使用的指令说明:
@yield('sectionName', 'defaultValue'): 显示给定 section 的内容,如果 section 未定义则显示默认值。@section('sectionName') ... @endsection: 定义内容 section。@show: 定义一个 section 并立即输出其内容。对于子视图可能会向其追加内容的侧边栏很有用。
步骤 2: 创建一个继承此主布局的子页面。将文件保存到 resources/views/pages/home.blade.php:
@extends('layouts.app')
@section('title', 'Homepage - My App')@section('header_title', 'Home Sweet Home')
@section('sidebar') @parent <!-- This will include the content from the parent's sidebar section --> <p>This is appended to the master sidebar from the homepage.</p>@endsection
@section('content') <h2>{{ $pageHeading }}</h2> <p>This is the main content for the homepage. Welcome, {{ $userName ?? 'Guest' }}!</p>@endsection子视图中使用的指令说明:
@extends('layouts.app'): 指定此视图继承layouts.app主布局。@section('title', 'Page Title'): 定义由主布局中的@yield定义的 ‘title’ section 的内容。@parent: 在 section 内时,@parent将内容追加(或前置)到父布局的 section 内容。??运算符是空值合并运算符 (null coalescing operator),如果$userName未设置则提供默认值。
步骤 3: 在 routes/web.php 中设置一个路由来显示此页面:
Route::get('/home', function () { return view('pages.home', [ 'pageHeading' => 'Welcome to Our Homepage', 'userName' => 'Jane Doe' ]);});步骤 4: 在浏览器中访问 http://localhost:8000/home。你将看到主页内容渲染在主布局结构内,其标题、侧边栏和内容 section 从 home.blade.php 填充。
Blade 还提供了许多其他特性,包括组件 (components)、循环 (loops)、条件语句 (conditional statements) 和包含子视图 (including sub-views)。请查阅官方 Laravel Blade 文档以获得全面指南。