翻译进度
8
分块数量
2
参与人数

安装

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。


安装

认识 Laravel

Laravel 是一个 Web 应用框架, 有着表现力强、语法优雅的特点。Web 框架为创建应用提供了一个结构和起点,你只需要专注于创造,我们来为你处理细节。

Laravel 致力于提供出色的开发体验,同时提供强大的特性,例如完全的依赖注入,富有表现力的数据库抽象层,队列和计划任务,单元和集成测试等等。

无论你是刚刚接触 PHP Web 框架的新人,亦或是有着多年经验的老手, Laravel 都是一个可以与你一同成长的框架。我们将帮助你迈出成为 Web 开发者的第一步,或是将你的经验提高到下一个等级。我们迫不及待的想看看你的作品。

为什么选择 Laravel?

有非常多的工具和框架可以被用于构建一个 Web 应用。但我们相信 Laravel 是构建现代化、全栈 Web 应用的最佳选择。

一个渐进式框架

我们喜欢称 Laravel 是一个「渐进式」框架。意思是 Laravel 将与你一同成长。如果你是首次进入 Web 开发, Laravel 大量的文档、指南和 视频教程 将帮助你熟悉使用技巧而不至于不知所措。

learn小辉 翻译于 6小时前

如果你是高级开发人员,Laravel 为你提供了强大的工具用于 依赖注入、 单元测试队列广播,等等。 Laravel 为构建专业的 Web 应用程序进行了微调,并准备好处理企业工作负载。

一个渐进式框架

Laravel 具有难以置信的可扩展性。由于 PHP 的灵活性以及 Laravel 对 Redis 等快速分布式缓存系统的内置支持,使用 Laravel 实现水平扩展是轻而易举的事。事实上,Laravel 应用已经很容易扩展到每月处理数亿个请求。

需极致扩展?Laravel Cloud 等平台能让你的应用实现近乎无限扩展。

一个社区化的框架

Laravel 结合了 PHP 生态系统中最好的软件包,提供了最健壮、对开发人员友好的框架。此外,来自世界各地的数千名有才华的开发人员 为框架做出贡献。谁知道呢,也许你就是下一个 Laravel 贡献者。

创建 Laravel 应用

安装 PHP 和 Laravel 安装器

在创建首个 Laravel 应用前,请确保本地环境已安装 PHPComposer 及 Laravel 安装器。此外,你需安装 Node 和 NPM 或 Bun,以编译应用的前端资源。

若本地尚未安装 PHP 和 Composer,以下命令可在 macOS、Windows 或 Linux 上安装 PHP、Composer 和 Laravel 安装器:

# macOS
/bin/bash -c "$(curl -fsSL https://php.new/install/mac/8.4)"
# Windows PowerShell
# 以管理员身份运行...
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://php.new/install/windows/8.4'))
# Linux
/bin/bash -c "$(curl -fsSL https://php.new/install/linux/8.4)"
Trace92 翻译于 6小时前

运行上述命令后,请重启终端会话。若需更新通过 php.new 安装的 PHP、Composer 和 Laravel 安装器,可在终端重新运行该命令。

若已安装 PHP 与 Composer,可通过以下命令安装 Laravel 安装器:

composer global require laravel/installer

[!NOTE]
如需图形化 PHP 安装与管理工具,请查看 Laravel Herd

创建应用

安装 PHP、Composer 和 Laravel 安装器后,执行以下命令创建新 Laravel 应用。Laravel 安装器将引导你选择测试框架、数据库及初始包及入门套件:

laravel new example-app

应用创建完成后,可使用 dev Composer 脚本启动 Laravel 本地开发服务器、队列及 Vite 开发服务器:

cd example-app
npm install && npm run build
composer run dev

开发服务器启动后,你就可以通过 localhost:8000 访问你的应用。接下来,你就可以开始进入 Laravel 生态的下一步。当然,你可能还想配置数据库

[!NOTE]
若想在开发 Laravel 应用时抢占先机,可考虑使用我们的入门套件。Laravel 入门套件为新应用提供后端与前端认证脚手架。

初始配置

Laravel 框架的所有配置文件存放于 config 目录。每项配置均有文档说明,你可自由浏览文件,熟悉可用选项。

Trace92 翻译于 6小时前

Laravel 开箱可用,不需要额外配置,你可以自由地开发!然而,你可能希望查看 config/app.php 文件及其文档。其中包含如 urllocale 等选项,你可根据应用需求调整。

基于环境的配置

由于 Laravel 的许多配置选项值可能会根据应用程序是在本地机器上运行还是在生产 Web 服务器上运行而有所不同,因此许多重要的配置选项值是在应用程序根目录的 .env 文件中定义的。

你的 .env 文件不应该提交到应用程序的源代码控制中,因各开发者或服务器可能需不同环境配置。此外,如果入侵者访问了你的源代码仓库,这将成为安全风险,因为任何敏感数据都会被公开。

[!NOTE]
若要了解更多关于 .env 文件和基于环境的配置的信息,请查看完整的 配置文档

数据库和迁移

现在,你已经创建了 Laravel 应用程序,可能想在数据库中存储一些数据。默认情况下,你的应用程序的 .env 配置文件指定 Laravel 将与 SQLite 数据库交互。

应用创建期间,Laravel 为你生成了 database/database.sqlite 文件,并运行了必要的迁移以创建应用数据库表。

如果你更想使用其他数据库驱动,例如 MySQL 或者 PostgreSQL,可更新 .env 配置文件以使用相应数据库。例如,若使用 MySQL,可如此更新 .env 文件中的 DB_* 变量:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Trace92 翻译于 6小时前

如果你选择使用 SQLite 以外的数据库,则需要创建数据库并运行应用程序的 数据库迁移:

php artisan migrate

[!NOTE]
如果你在 macOS 或 Windows 上进行开发,并且需要在本地安装 MySQL、PostgreSQL 或 Redis,请考虑使用 Herd ProDBngin

目录配置

Laravel 应始终从为 Web 服务器配置的 「Web 目录」根目录提供服务。不应尝试从「Web 目录」的子目录提供 Laravel 应用服务,否则可能暴露应用中的敏感文件。

使用 Herd 进行本地安装

Laravel Herd 是一个为 macOS 和 Windows 打造的极速本地 Laravel 和 PHP 开发环境。Herd 包含开发 Laravel 所需的一切,包括 PHP 和 Nginx。

安装 Herd 后,你即可开始 Laravel 开发。Herd 提供命令行工具,包括 phpcomposerlaravelexposenodenpm 和 nvm

[!NOTE]
Herd Pro 为 Herd 增添了额外的强大功能,例如创建和管理本地 MySQL、Postgres 和 Redis 数据库的能力,以及本地邮件查看和日志监控的能力。

macOS 上的 Herd

如果你在 macOS 上进行开发,可以从 Herd website 下载 Herd 安装程序。安装程序会自动下载最新版本的 PHP,并将你的 Mac 配置为始终在后台运行 Nginx

Trace92 翻译于 6小时前

MacOS 版 Herd 使用 dnsmasq 支持 「parked」 目录。parked 目录中的任何 Laravel 应用程序都将自动由 Herd 提供服务。默认情况下,Herd 在 ~/Herd 处创建一个 parked 目录,你可以使用其目录名称在 .test 域上访问此目录中的任何 Laravel 应用程序。

安装 Herd 后,创建新 Laravel 项目的最快方法是使用与 Herd 捆绑在一起的 Laravel CLI:

cd ~/Herd
laravel new my-app
cd my-app
herd open

当然,你也可以通过 Herd 的 UI 管理你的 parked 目录和其他 PHP 设置,该 UI 可以从系统托盘中的 Herd 菜单打开。

可以通过查看 Herd 文档 了解有关 Herd 的更多信息。

Windows 上的使用 Herd

你可以在 Herd 网站 下载 Herd 的 Windows 安装程序。安装完成后,你可以启动 Herd 以完成入门流程并首次访问 Herd UI。

左键单击 Herd 的系统托盘图标即可访问 Herd UI。右键单击可打开快捷菜单,其中可访问你日常所需的所有工具。

在安装过程中,Herd 会在你的主目录中的 %USERPROFILE%\Herd 创建一个 「parked」 目录。 parked 目录中的任何 Laravel 应用程序都将自动由 Herd 提供服务,并且你可以使用其目录名称在 .test 域上访问此目录中的任何 Laravel 应用程序。

安装 Herd 后,创建新 Laravel 项目的最快方法是使用与 Herd 捆绑在一起的 Laravel CLI。首先,打开 Powershell 并运行以下命令:

cd ~\Herd
laravel new my-app
cd my-app
herd open
Trace92 翻译于 6小时前

You can learn more about Herd by checking out the Herd documentation for Windows.

IDE Support

You are free to use any code editor you wish when developing Laravel applications; however, PhpStorm offers extensive support for Laravel and its ecosystem, including Laravel Pint.

In addition, the community maintained Laravel Idea PhpStorm plugin offers a variety of helpful IDE augmentations, including code generation, Eloquent syntax completion, validation rule completion, and more.

If you develop in Visual Studio Code (VS Code), the official Laravel VS Code Extension is now available. This extension brings Laravel-specific tools directly into your VS Code environment, enhancing productivity.

Next Steps

Now that you have created your Laravel application, you may be wondering what to learn next. First, we strongly recommend becoming familiar with how Laravel works by reading the following documentation:

How you want to use Laravel will also dictate the next steps on your journey. There are a variety of ways to use Laravel, and we'll explore two primary use cases for the framework below.

Laravel the Full Stack Framework

Laravel may serve as a full stack framework. By "full stack" framework we mean that you are going to use Laravel to route requests to your application and render your frontend via Blade templates or a single-page application hybrid technology like Inertia. This is the most common way to use the Laravel framework, and, in our opinion, the most productive way to use Laravel.

If this is how you plan to use Laravel, you may want to check out our documentation on frontend development, routing, views, or the Eloquent ORM. In addition, you might be interested in learning about community packages like Livewire and Inertia. These packages allow you to use Laravel as a full-stack framework while enjoying many of the UI benefits provided by single-page JavaScript applications.

If you are using Laravel as a full stack framework, we also strongly encourage you to learn how to compile your application's CSS and JavaScript using Vite.

[!NOTE]
If you want to get a head start building your application, check out one of our official application starter kits.

Laravel the API Backend

Laravel may also serve as an API backend to a JavaScript single-page application or mobile application. For example, you might use Laravel as an API backend for your Next.js application. In this context, you may use Laravel to provide authentication and data storage / retrieval for your application, while also taking advantage of Laravel's powerful services such as queues, emails, notifications, and more.

If this is how you plan to use Laravel, you may want to check out our documentation on routing, Laravel Sanctum, and the Eloquent ORM.

本文章首发在 LearnKu.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
贡献者:2
讨论数量: 14
发起讨论 只看当前版本


light-and-dark
excel导出
1 个点赞 | 5 个回复 | 代码速记 | 课程版本 8.5
admin7742
CURLPOST
0 个点赞 | 5 个回复 | 代码速记 | 课程版本 8.x
SnowZh
使用 Laravel sail 如何安装指定版本?
0 个点赞 | 2 个回复 | 问答 | 课程版本 8.5
abc123123123456
敏感词过滤
0 个点赞 | 1 个回复 | 分享 | 课程版本 8.5
abc123123123456
七牛云扩展
0 个点赞 | 0 个回复 | 分享 | 课程版本 8.5
kang_lin
比较明亮的显示
0 个点赞 | 0 个回复 | 代码速记 | 课程版本 8.5
jianghushusheng
图片分割
0 个点赞 | 0 个回复 | 代码速记 | 课程版本 8.x
esssd
date 高亮显示
0 个点赞 | 0 个回复 | 代码速记 | 课程版本 8.x
jianghushusheng
gcms
0 个点赞 | 0 个回复 | 代码速记 | 课程版本 8.x
hei123
php artisan make:command withdraw
0 个点赞 | 0 个回复 | 代码速记 | 课程版本 8.x
Queen_Lr
发送验证码
0 个点赞 | 0 个回复 | 分享 | 课程版本 8.x
XiaoSir
图片防盗链
0 个点赞 | 0 个回复 | 代码速记 | 课程版本 8.x
anningri
laravel new 不出东西来
0 个点赞 | 0 个回复 | 问答 | 课程版本 5.8
jiayier
如何让 API 的返回数据格式统一(Laravel5.8)
0 个点赞 | 0 个回复 | 问答 | 课程版本 5.8