首页 > PHP > Laravel教程 视图

Laravel教程 视图

2025-02-17 14:24:43
在 Laravel 中,视图是用来分离业务逻辑和前端展示的重要组成部分,它主要负责将数据以美观、合适的方式呈现给用户。除了使用 Blade 模板引擎,你也可以直接使用原生 PHP 作为模板。以下是一份详细的 Laravel 视图教程:

1. 视图基础

1.1 视图文件的位置

在 Laravel 里,默认的视图文件存放在 resources/views 目录下。你可以在这个目录下创建子目录来组织视图文件,这样能让项目结构更加清晰。

1.2 创建视图文件

视图文件的扩展名为 .blade.php,这是 Laravel 提供的 Blade 模板引擎的文件格式。例如,在 resources/views 目录下创建一个名为 welcome.blade.php 的文件,内容如下:
 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome Page</title>
</head>
<body>
    <h1>Welcome to my Laravel application!</h1>
</body>
</html>

1.3 渲染视图

在控制器或者路由中,你可以使用 view 函数来渲染视图。以下是在路由中渲染 welcome.blade.php 视图的示例:
 
 
// routes/web.php
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

 

这里的 view('welcome') 会自动查找 resources/views/welcome.blade.php 文件并将其内容返回给用户。

2. 传递数据到视图

2.1 单个数据传递

你可以通过 view 函数的第二个参数传递数据到视图。例如:
 
 
 
 
// routes/web.php
Route::get('/greet', function () {
    $name = 'John';
    return view('greet', ['name' => $name]);
});

 

然后在 resources/views/greet.blade.php 视图文件中使用这个数据:
 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Greeting</title>
</head>
<body>
    <h1>Hello, {{ $name }}!</h1>
</body>
</html>

 

在 Blade 模板中,使用 {{ }} 语法来输出变量的值。

2.2 多个数据传递

可以传递多个变量到视图,只需要在数组中添加更多的键值对即可:
 
 
Route::get('/user', function () {
    $user = [
        'name' => 'Jane',
        'age' => 25
    ];
    return view('user', $user);
});

 

在 resources/views/user.blade.php 视图文件中使用这些数据:
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Information</title>
</head>
<body>
    <h1>User Name: {{ $name }}</h1>
    <p>User Age: {{ $age }}</p>
</body>
</html>

3. Blade 模板引擎

3.1 模板继承

Blade 支持模板继承,允许你创建一个基础模板,然后在其他视图中继承它。

 

基础模板(resources/views/layouts/app.blade.php
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@yield('title', 'Default Title')</title>
</head>
<body>
    <header>
        <h1>My Laravel App</h1>
    </header>
    <main>
        @yield('content')
    </main>
    <footer>
        <p>&copy; 2025 My Laravel App</p>
    </footer>
</body>
</html>

 

在基础模板中,@yield 指令用于定义可以被子视图填充的区域。
子视图(resources/views/home.blade.php
 
@extends('layouts.app')

@section('title', 'Home Page')

@section('content')
    <h2>Welcome to the Home Page</h2>
    <p>This is the content of the home page.</p>
@endsection

 

@extends 指令用于指定要继承的基础模板,@section 指令用于填充基础模板中定义的区域。

3.2 条件语句和循环语句

  • 条件语句
 
@if ($age >= 18)
    <p>You are an adult.</p>
@else
    <p>You are a minor.</p>
@endif
  • 循环语句
 
@php
    $fruits = ['apple', 'banana', 'cherry'];
@endphp

<ul>
    @foreach ($fruits as $fruit)
        <li>{{ $fruit }}</li>
    @endforeach
</ul>

4. 视图组件

4.1 创建视图组件

可以使用 Artisan 命令创建视图组件:
 
 
php artisan make:component Alert
这会在 app/View/Components 目录下创建一个 Alert.php 类,同时在 resources/views/components 目录下创建一个 alert.blade.php 视图文件。

4.2 使用视图组件

在其他视图中使用组件:
 
 
<x-alert>
    This is an important alert!
</x-alert>
在 resources/views/components/alert.blade.php 中可以定义组件的具体样式:
 
<div class="alert alert-info">
    {{ $slot }}
</div>

 

$slot 变量用于获取组件标签内的内容。

 

通过以上步骤,你可以掌握 Laravel 视图的基本使用方法,包括视图的创建、数据传递、模板继承、条件和循环语句以及视图组件的使用。
使用 Ctrl+D 可将网站添加到书签
收藏网站
扫描二维码
关注早实习微信公众号
官方公众号
Top