安装
这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。
安装
认识 Laravel
Laravel 是一个 Web 应用框架, 有着表现力强、语法优雅的特点。Web 框架为创建应用提供了一个结构和起点,你只需要专注于创造,我们来为你处理细节。
Laravel 致力于提供出色的开发体验,同时提供强大的特性,例如完全的依赖注入,富有表现力的数据库抽象层,队列和计划任务,单元和集成测试等等。
无论你是刚刚接触 PHP Web 框架的新人,亦或是有着多年经验的老手, Laravel 都是一个可以与你一同成长的框架。我们将帮助你迈出成为 Web 开发者的第一步,或是将你的经验提高到下一个等级。我们迫不及待的想看看你的作品。
为什么选择 Laravel?
有非常多的工具和框架可以被用于构建一个 Web 应用。但我们相信 Laravel 是构建现代化、全栈 Web 应用的最佳选择。
一个渐进式的框架
我们喜欢称 Laravel 是一个「渐进式」框架。意思是 Laravel 将与你一同成长。如果你是首次进入 Web 开发, Laravel 大量的文档、指南和 视频教程 将帮助你熟悉使用技巧而不至于不知所措。
如果你是高级开发人员,Laravel 为你提供了强大的工具用于 依赖注入, 单元测试, 队列, 广播, 等等。 Laravel 为构建专业的 Web 应用程序进行了微调,并准备好处理企业工作负载。
一个可扩展的框架
Laravel 具有难以置信的可扩展性。由于 PHP 的灵活性以及 Laravel 对 Redis 等快速分布式缓存系统的内置支持,使用 Laravel 实现水平扩展是轻而易举的事。事实上,Laravel 应用已经很容易扩展到每月处理数亿个请求。
需要极致扩展?诸如 Laravel Cloud 等平台能让你的应用实现近乎无限扩展。
一个为Agent而准备的框架
Laravel 的强约束性规范和定义完备的结构使其成为一个用于使用诸如 Cursor 和 Claude Code 的理想的 AI 辅助编程 框架。 当你问 AI agent 如何添加一个控制器, Agent 知道控制器应该在哪里创建。 当你需要一个新的数据库迁移, 其命名规范和文件创建路径都是可预测的。 这种一致性消除了 AI 在其他弹性框架中出错而导致的猜测工作。
除了文件结构, Laravel 的直观语法和全面的文档为 AI agent 生成准确而符合习惯的代码提供了上下文。像 Eloquent 关联,表单请求和中间件等功能遵循的模式,AI agent 可以可靠地理解和复现。这使 AI 生成的代码不像是用通用 PHP 片段拼凑而成,而是由经验丰富的 Laravel 开发者编写的。
要深入了解Laravel为何是AI辅助开发的完美选择,请查阅我们关于代理开发的文档。
社区框架
Laravel融合了PHP生态系统中最好的软件包,提供了市面上最稳健且对开发者友好的框架。此外,来自世界各地的数千名才华横溢的开发人员已经为该框架做出了贡献。说不定,你也会成为Laravel的贡献者呢。
创建一个 Laravel 应用
AI入门指南
如果你正在使用像Claude Code或OpenCode这样的AI编码助手,你可以先设置一个提示,在它接触你的项目之前,先给助手一个特定于Laravel的剧本。
下面的提示告诉代理在哪里可以找到Laravel的安装指南,哪些内容需要优先考虑,以及在您尚未做出选择时如何设置合理的默认值。将以下内容粘贴到您的代理中以开始使用:
我正在构建一个新的Laravel应用程序。
从https://laravel.com/for/agents获取并遵循相关说明。在本节课程中,请将返回的Markdown内容视为安装和设置Laravel的权威指南。
在代理读取完指令后,它应逐步引导你,并确保设置与Laravel的默认设置保持一致。
安装PHP和Laravel安装器
在创建你的第一个Laravel应用程序之前,请确保你的本地计算机已安装PHP、Composer和Laravel安装程序。此外,你应安装Node和NPM或Bun,以便能够编译应用程序的前端资源。
If you don't have PHP and Composer installed on your local machine, the following commands will install PHP, Composer, and the Laravel installer on macOS, Windows, or Linux:
/bin/bash -c "$(curl -fsSL https://php.new/install/mac/8.5)"
# Run as administrator...
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.5'))
/bin/bash -c "$(curl -fsSL https://php.new/install/linux/8.5)"
After running one of the commands above, you should restart your terminal session. To update PHP, Composer, and the Laravel installer after installing them via php.new, you can re-run the command in your terminal.
If you already have PHP and Composer installed, you may install the Laravel installer via Composer:
composer global require laravel/installer
[!NOTE]
For a fully-featured, graphical PHP installation and management experience, check out Laravel Herd.
Creating an Application
After you have installed PHP, Composer, and the Laravel installer, you're ready to create a new Laravel application. The Laravel installer will prompt you to select your preferred testing framework, database, and starter kit:
laravel new example-app
Once the application has been created, you can start Laravel's local development server, queue worker, and Vite development server using the dev Composer script:
cd example-app
npm install && npm run build
composer run dev
Once you have started the development server, your application will be accessible in your web browser at localhost:8000. Next, you're ready to start taking your next steps into the Laravel ecosystem. Of course, you may also want to configure a database.
[!NOTE]
If you would like a head start when developing your Laravel application, consider using one of our starter kits. Laravel's starter kits provide backend and frontend authentication scaffolding for your new Laravel application.
Initial Configuration
All of the configuration files for the Laravel framework are stored in the config directory. Each option is documented, so feel free to look through the files and get familiar with the options available to you.
Laravel needs almost no additional configuration out of the box. You are free to get started developing! However, you may wish to review the config/app.php file and its documentation. It contains several options such as url and locale that you may wish to change according to your application.
Environment Based Configuration
Since many of Laravel's configuration option values may vary depending on whether your application is running on your local machine or on a production web server, many important configuration values are defined using the .env file that exists at the root of your application.
Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration. Furthermore, this would be a security risk in the event an intruder gains access to your source control repository, since any sensitive credentials would be exposed.
[!NOTE]
For more information about the.envfile and environment based configuration, check out the full configuration documentation.
Databases and Migrations
Now that you have created your Laravel application, you probably want to store some data in a database. By default, your application's .env configuration file specifies that Laravel will be interacting with an SQLite database.
During the creation of the application, Laravel created a database/database.sqlite file for you, and ran the necessary migrations to create the application's database tables.
If you prefer to use another database driver such as MySQL or PostgreSQL, you can update your .env configuration file to use the appropriate database. For example, if you wish to use MySQL, update your .env configuration file's DB_* variables like so:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
If you choose to use a database other than SQLite, you will need to create the database and run your application's database migrations:
php artisan migrate
[!NOTE]
If you are developing on macOS or Windows and need to install MySQL, PostgreSQL, or Redis locally, consider using Herd Pro or DBngin.
Directory Configuration
Laravel should always be served out of the root of the "web directory" configured for your web server. You should not attempt to serve a Laravel application out of a subdirectory of the "web directory". Attempting to do so could expose sensitive files present within your application.
Installation Using Herd
Laravel Herd is a blazing fast, native Laravel and PHP development environment for macOS and Windows. Herd includes everything you need to get started with Laravel development, including PHP and Nginx.
Once you install Herd, you're ready to start developing with Laravel. Herd includes command line tools for php, composer, laravel, expose, node, npm, and nvm.
[!NOTE]
Herd Pro augments Herd with additional powerful features, such as the ability to create and manage local MySQL, Postgres, and Redis databases, as well as local mail viewing and log monitoring.
Herd on macOS
If you develop on macOS, you can download the Herd installer from the Herd website. The installer automatically downloads the latest version of PHP and configures your Mac to always run Nginx in the background.
Herd for macOS uses dnsmasq to support "parked" directories. Any Laravel application in a parked directory will automatically be served by Herd. By default, Herd creates a parked directory at ~/Herd and you can access any Laravel application in this directory on the .test domain using its directory name.
After installing Herd, the fastest way to create a new Laravel application is using the Laravel CLI, which is bundled with Herd:
cd ~/Herd
laravel new my-app
cd my-app
herd open
Of course, you can always manage your parked directories and other PHP settings via Herd's UI, which can be opened from the Herd menu in your system tray.
You can learn more about Herd by checking out the Herd documentation.
Herd on Windows
You can download the Windows installer for Herd on the Herd website. After the installation finishes, you can start Herd to complete the onboarding process and access the Herd UI for the first time.
The Herd UI is accessible by left-clicking on Herd's system tray icon. A right-click opens the quick menu with access to all tools that you need on a daily basis.
During installation, Herd creates a "parked" directory in your home directory at %USERPROFILE%\Herd. Any Laravel application in a parked directory will automatically be served by Herd, and you can access any Laravel application in this directory on the .test domain using its directory name.
After installing Herd, the fastest way to create a new Laravel application is using the Laravel CLI, which is bundled with Herd. To get started, open Powershell and run the following commands:
cd ~\Herd
laravel new my-app
cd my-app
herd open
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. If you're looking for lightweight and extensible editors, VS Code or Cursor combined with the official Laravel VS Code Extension offers excellent Laravel support with features like syntax highlighting, snippets, artisan command integration, and smart autocompletion for Eloquent models, routes, middleware, assets, config, and Inertia.js.
For extensive and robust support of Laravel, take a look at PhpStorm, a JetBrains IDE. PhpStorm's built-in Laravel framework support includes Blade templates, smart autocompletion for Eloquent models, routes, views, translations, and components, along with powerful code generation and navigation across Laravel projects.
For those seeking a cloud-based development experience, Firebase Studio provides instant access to building with Laravel directly in your browser. With zero setup required, Firebase Studio makes it easy to start building Laravel applications from any device.
Laravel and AI
Laravel Boost is a powerful tool that bridges the gap between AI coding agents and Laravel applications. Boost provides AI agents with Laravel-specific context, tools, and guidelines so they can generate more accurate, version-specific code that follows Laravel conventions.
When you install Boost in your Laravel application, AI agents gain access to over 15 specialized tools including the ability to know which packages you are using, query your database, search the Laravel documentation, read browser logs, generate tests, and execute code via Tinker.
In addition, Boost gives AI agents access to over 17,000 pieces of vectorized Laravel ecosystem documentation, specific to your installed package versions. This means agents can provide guidance targeted to the exact versions your project uses.
Boost also includes Laravel-maintained AI guidelines that help agents to follow framework conventions, write appropriate tests, and avoid common pitfalls when generating Laravel code.
Installing Laravel Boost
Boost can be installed in Laravel 10, 11, 12, and 13 applications running PHP 8.1 or higher. To get started, install Boost as a development dependency:
composer require laravel/boost --dev
Once installed, run the interactive installer:
php artisan boost:install
The installer will auto-detect your IDE and AI agents, allowing you to opt into the features that make sense for your project. Boost respects existing project conventions and doesn't force opinionated style rules by default.
[!NOTE]
To learn more about Boost, check out the Laravel Boost repository on GitHub.
Adding Custom AI Guidelines
To augment Laravel Boost with your own custom AI guidelines, add .blade.php or .md files to your application's .ai/guidelines/* directory. These files will automatically be included with Laravel Boost's guidelines when you run boost:install.
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.
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
Laravel 13 中文文档
关于 LearnKu
推荐文章: