
Building Web Applications with PHP and Laravel
Getting Started with Laravel
To begin, ensure you have PHP and Composer installed on your machine. Composer is a dependency manager for PHP that will help you install Laravel and its dependencies. You can check your PHP version by running:
php -vTo install Laravel, use Composer to create a new project:
composer create-project --prefer-dist laravel/laravel my-laravel-appThis command will create a new directory named my-laravel-app with the latest version of Laravel.
Directory Structure
Once the installation is complete, navigate to the project directory:
cd my-laravel-appThe directory structure of a Laravel application is as follows:
my-laravel-app/
├── app/
│ ├── Http/
│ ├── Models/
│ └── ...
├── bootstrap/
├── config/
├── database/
├── public/
├── resources/
├── routes/
├── storage/
└── tests/- app/: Contains the core code of the application.
- routes/: Contains all route definitions.
- public/: The entry point for the web server.
Routing in Laravel
Routing in Laravel is straightforward. Open the routes/web.php file to define your web routes. Here’s an example of a simple route:
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});This route will return the welcome view when the root URL is accessed. You can create more complex routes with parameters:
Route::get('/user/{id}', function ($id) {
return 'User ' . $id;
});Controllers
For better organization, you can use controllers to handle the logic for your routes. To create a controller, use the Artisan command line tool:
php artisan make:controller UserControllerThis command creates a new controller in the app/Http/Controllers directory. You can define methods within your controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function show($id)
{
return 'User ' . $id;
}
}Then, you can link this controller method to a route:
Route::get('/user/{id}', [UserController::class, 'show']);Database Migrations
Laravel provides a powerful migration system for managing your database schema. To create a migration, use the following Artisan command:
php artisan make:migration create_users_tableThis command creates a new migration file in the database/migrations directory. Open the newly created file and define the schema:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}To run the migration and create the table, execute:
php artisan migrateEloquent ORM
Laravel's Eloquent ORM provides a simple ActiveRecord implementation for working with your database. To create a model for the users table, run:
php artisan make:model UserYou can then use the User model to interact with the database:
use App\Models\User;
// Create a new user
$user = new User();
$user->name = 'John Doe';
$user->email = '[email protected]';
$user->save();
// Retrieve all users
$users = User::all();Blade Templating Engine
Laravel includes a powerful templating engine called Blade. You can create a Blade template in the resources/views directory. For example, create a file named users.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>Users</title>
</head>
<body>
<h1>Users List</h1>
<ul>
@foreach ($users as $user)
<li>{{ $user->name }}</li>
@endforeach
</ul>
</body>
</html>To return this view from a route, you can pass data to it:
Route::get('/users', function () {
$users = User::all();
return view('users', ['users' => $users]);
});Conclusion
Laravel is a robust framework that streamlines the development process for PHP applications. By following the steps outlined in this tutorial, you can set up a basic web application, manage routes and controllers, utilize Eloquent ORM for database interactions, and leverage Blade for templating.
