You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel新手求助:如何启动并运行我的第一个程序?

Hey there! No worries at all—getting started with Laravel is totally doable even if you're new. Let's walk through this step by step, nice and slow.

Getting Started with Your First Laravel Program

1. Confirm You Have the Required Tools

First, Laravel needs a few core tools to run smoothly. Let's make sure you have these:

  • PHP (version 8.1 or higher—check by running php -v in your terminal)
  • Composer (Laravel's package manager; if you don't have it, install it directly via your terminal)
  • A code editor (like VS Code, Sublime Text, or whatever you prefer)
  • Optional: A database (MySQL/PostgreSQL) for later projects, but we can skip this for your first test

2. Install a New Laravel Project

Open your terminal (Command Prompt on Windows, Terminal on Mac/Linux) and pick one of these methods:

Using Composer Directly

composer create-project laravel/laravel my-first-laravel-app

This will create a folder named my-first-laravel-app with all Laravel's core files.

Using the Laravel Installer (For Faster Future Projects)

First, install the Laravel installer globally:

composer global require laravel/installer

Then create your project with:

laravel new my-first-laravel-app

3. Move Into Your Project Folder

Once installation finishes, navigate to your project directory:

cd my-first-laravel-app

4. Start the Built-in Development Server

Laravel has a handy built-in server so you don't need to set up Apache/Nginx right away. Run this command:

php artisan serve

You'll see a message like:

Laravel development server started on http://127.0.0.1:8000

Open that URL in your browser—you'll see Laravel's default welcome page! That means your setup is working.

5. Build Your First Custom Program

Let's make something simple to test things out.

Option 1: Simple Text Response

Open the routes/web.php file in your code editor. Add this line at the bottom:

Route::get('/hello', function () {
    return 'Hello, this is my first Laravel program! 🎉';
});

Save the file, then visit http://127.0.0.1:8000/hello in your browser. You'll see your custom message!

Option 2: Basic HTML Page (Using Blade Templating)

If you want to show a proper HTML page:

  1. Create a new file in resources/views called hello.blade.php
  2. Add some basic HTML:
<!DOCTYPE html>
<html>
<head>
    <title>My First Laravel Page</title>
</head>
<body>
    <h1>Hello from Laravel!</h1>
    <p>This is my first custom page using Laravel's Blade templating system.</p>
</body>
</html>
  1. Update the route in routes/web.php to load this view:
Route::get('/hello', function () {
    return view('hello');
});

Save everything, refresh the /hello page—you'll see your HTML page live!

6. Quick Troubleshooting Tips

  • If php artisan serve fails, double-check that PHP is installed and added to your system's PATH.
  • If Composer throws errors, ensure you have a stable internet connection and Composer is installed correctly.
  • Always check terminal error messages—Laravel gives clear hints about what's going wrong.

You've got this! Once you're comfortable with routes and views, you can start exploring controllers, databases, and all the powerful features Laravel offers.

内容的提问来源于stack exchange,提问作者Selva Meena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:57:33