PHP Laravel Basics

πŸš€ 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

Try It Now

🎯 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

Try It Now

πŸ” 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!";
});

Try It Now

🎯 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

Try It Now

πŸ“ 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!";
    }
}

Try It Now

Then, update your routes/web.php file to use this controller:

use App\Http\Controllers\MyController;

Route::get('/greet', [MyController::class, 'greet']);

Try It Now

🎯 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>

Try It Now

Modify the route to return this view:

Route::get('/', function () {
    return view('welcome');
});

Try It Now

πŸ” 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! πŸ—οΈ