在 CentOS 上使用 Laravel 渲染视图,你需要遵循以下步骤:
composer create-project --prefer-dist laravel/laravel your_project_name
将 your_project_name 替换为你的项目名称。
resources/views 目录下。在该目录下创建一个新的视图文件,例如 welcome.blade.php。在这个文件中编写你的 HTML 代码和 Blade 模板语法。例如,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</title>
</head>
<body>
<h1>Welcome to {{ $name }}</h1>
</body>
</html>
app/Http/Controllers 目录下的一个控制器文件,例如 HomeController.php。在该文件中,编写一个方法来渲染视图。<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
$name = 'John Doe';
return view('welcome', ['name' => $name]);
}
}
在这个例子中,我们将 $name 变量传递给视图。
routes/web.php 文件中配置一个路由,以便用户可以访问刚刚创建的视图。在文件中添加以下代码:use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index']);
现在,当你访问应用程序的根 URL(例如 http://your_project_domain/)时,Laravel 将渲染 welcome.blade.php 视图,并显示 “Welcome to John Doe” 的消息。
这就是在 CentOS 上使用 Laravel 渲染视图的基本过程。你可以根据需要创建更多的视图和控制器,并在路由中进行配置。