π PHP Laravel Basics – Getting Started with Laravel
Laravel is one of the most powerful PHP frameworks for building modern web applications. It makes coding faster, cleaner, and more enjoyable with built-in tools like routing, authentication, and database handling. Let’s dive into Laravel! πββοΈ
π What is Laravel?
Laravel is an open-source PHP framework designed for web development. It follows the MVC (Model-View-Controller) pattern, making applications well-structured and easy to manage.
- Blade Templating: Laravel provides a simple and powerful templating engine.
- Eloquent ORM: A great way to interact with databases using models.
- Built-in Authentication: User authentication is super easy with Laravel.
- Routing & Middleware: Handles web routes and security efficiently.
π Step 1: Install Laravel
β Installing Laravel via Composer
Before installing Laravel, ensure you have Composer installed. Then, run this command:
composer create-project laravel/laravel myApp
π― This will: Download Laravel and set up a new project folder myApp
.
π Step 2: Start the Laravel Development Server
Once Laravel is installed, navigate to your project folder and start the local server:
cd myApp php artisan serve
π Output: Your Laravel app will be available at http://127.0.0.1:8000
.
π Step 3: Laravel Routing (Creating a Simple Route)
Routes in Laravel define how your application responds to HTTP requests. Open the routes/web.php
file and add a new route:
use Illuminate\Support\Facades\Route; Route::get('/hello', function () { return "Hello, Laravel!"; });
π― Now, visit: http://127.0.0.1:8000/hello
and see the message.
π Step 4: Creating a Controller
Laravel uses controllers to handle requests more efficiently. Create a new controller using:
php artisan make:controller MyController
π This creates: app/Http/Controllers/MyController.php
.
Now, open the controller and add a function:
namespace App\Http\Controllers; use Illuminate\Http\Request; class MyController extends Controller { public function greet() { return "Hello from Controller!"; } }
Then, update your routes/web.php
file to use this controller:
use App\Http\Controllers\MyController; Route::get('/greet', [MyController::class, 'greet']);
π― Now, visit: http://127.0.0.1:8000/greet
to see the output.
π Step 5: Creating a View (Blade Templates)
Laravel uses Blade Templates to make frontend rendering easy. Let’s create a new view:
<!-- resources/views/welcome.blade.php --> <!DOCTYPE html> <html> <head> <title>Welcome to Laravel</title> </head> <body> <h1>Hello, Laravel World!</h1> </body> </html>
Modify the route to return this view:
Route::get('/', function () { return view('welcome'); });
π Output: This renders the welcome page in Laravel.
π― Summary
- β Laravel is a powerful PHP framework following the MVC pattern.
- β
You can create routes in
routes/web.php
. - β Controllers help organize logic and respond to requests.
- β Blade templates make HTML rendering easier & faster.
π Next Steps
Now that you’ve learned the basics, try working with Laravel Models & Databases to build dynamic applications! ποΈ