Installation and routing
Create a project, learn the Artisan commands you will actually run, and define routes that stay readable.
Installation and the Artisan workflow
Laravel is a Composer package, so a project is a directory with a manifest and an artisan console at its root. PHP 8.2 or newer is required by current releases.
# create a project with Composer
composer create-project laravel/laravel example-app
cd example-app
php artisan serve # http://127.0.0.1:8000
php artisan about # versions, drivers and environment summary
php artisan route:list # every registered route and its action
php artisan migrate # apply pending database migrations
php artisan tinker # REPL with the framework booted| Command | Purpose |
|---|---|
php artisan serve | Run the local development server |
php artisan route:list | Show routes with URI, name, action and middleware |
php artisan make:model Post -mcr | Model plus migration, controller and resource routes |
php artisan migrate | Apply pending migrations |
php artisan db:seed | Run database seeders |
php artisan optimize:clear | Clear config, route, view and application caches |
php artisan test | Run the Pest or PHPUnit suite |
💡
Laravel caches configuration and routes. When an edit to
.env or a config file appears to have no effect, run php artisan optimize:clear — that one command explains most cases of the framework seeming to ignore you.Routing
<?php
// routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/', fn () => view('welcome'));
Route::middleware(['auth'])->group(function () {
Route::resource('posts', PostController::class);
Route::get('/dashboard', [PostController::class, 'index'])
->name('dashboard');
});
// parameters and constraints
Route::get('/users/{user}', [UserController::class, 'show'])
->whereNumber('user');- Routes live in
routes/web.php(session, cookies, CSRF protection) orroutes/api.php(stateless, prefixed with/api). Route::resourceregisters the seven RESTful actions and their names in one declaration.- Name routes and generate URLs with
route('posts.show', $post)so a path change touches one file instead of every template. - Route model binding resolves
{post}to aPostinstance by primary key, and returns 404 automatically when no row matches. - Group middleware, prefixes and domains rather than repeating the same chain on every route.
FAQ
web.php or api.php?
Web routes get the session, cookies and CSRF protection and render views. API routes are stateless and rate-limited by default, which is what a JSON client wants.
Why do I get a 419 page expired error?
The CSRF token is missing or stale on a state-changing request. Include
@csrf in the Blade form, or send the X-XSRF-TOKEN header from JavaScript.Related
Controllers, Blade views and Eloquent models Migrations and validation
Last refreshed 2026-09-18.