Introduction to Laravel
Laravel is a popular PHP framework that simplifies web application development with elegant syntax, robust tools, and efficient features. This tutorial will walk you through the basics of Laravel and help you get started with your first Laravel project.
Prerequisites
- Basic knowledge of PHP
- Composer installed on your system
- PHP installed (minimum version 8.0)
- A local development server like XAMPP, WAMP, or Laravel Homestead
Step 1: Install Laravel
To get started, you need to install Laravel using Composer. Open your terminal or command prompt and run the following command:
composer create-project --prefer-dist laravel/laravel myproject
This will create a new Laravel project named myproject
.
Step 2: Directory Structure
After installation, explore the Laravel project structure. Here are some key folders:
- app/: Contains the core application code (Controllers, Models, etc.)
- routes/: Defines the routes of the application.
- resources/: Contains views (Blade templates), assets, and localization files.
- config/: Holds configuration files.
- database/: For migrations, factories, and seeds.
Step 3: Configure Environment
Laravel uses the .env
file to manage environment-specific settings like database configuration, mail settings, etc. Open the .env
file and update your database settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
Make sure the database is created beforehand.
Step 4: Create a Route
Routes define how URLs map to specific actions or logic in the application. Open routes/web.php
and add the following:
Route::get('/', function () {
return view('welcome');
});
This route will display the welcome
view when you visit the home page (/
).
Step 5: Create a Controller
Laravel follows the MVC (Model-View-Controller) architecture. Let's create a simple controller to handle logic.
Run the following command to generate a new controller:
php artisan make:controller PageController
This creates PageController.php
in the app/Http/Controllers
directory. Open the controller and define a method:
Now, create a new route for this controller method in routes/web.php
:
Step 6: Create a View
Views are stored in the resources/views
directory. Let's create an about.blade.php
file.
Comments
Post a Comment