One-Hour Crash Course on Laravel for PHP Developers

Jun 7, 2024

One-Hour Crash Course on Laravel for PHP Developers

Introduction

  • Audience: Developers with decent knowledge in object-oriented programming and PHP.
  • Goal: Dive into Laravel by building a small personal Notes application.

Key Objectives

  1. Setup a working environment for Laravel development.
  2. Understand Laravel's file and folder structure.
  3. Learn how to change configurations and define roles in Laravel.
  4. Generate models, migrations, and controllers.
  5. Implement CRUD operations (Create, Read, Update, Delete) for notes.
  6. Manage sessions, CSRF tokens, and notifications.
  7. Add registration and login functionalities.
  8. Deploy Laravel projects to production environments.
  9. Utilize Artisan commands.
  10. Understand middleware and authentication.
  11. Integrate frontend elements and styles using Vite and Tailwind CSS.
  12. Perform data validation and email verification.
  13. Use Laravel Breeze for user authentication and verification.

Initial Setup

  • Dependencies: PHP (via XAMPP), Composer, Node.js, npm, Git, Git Bash
  • IDE: VS Code with specific extensions (PHP Intelephense, Laravel Blade snippets, etc.)
  • Command Execution:
    • composer create-project --prefer-dist laravel/laravel <project-name>
    • npm install
    • npm run dev

File and Folder Structure

  • Key Folders:
    • app: Contains HTTP, Models, and Providers.
    • config: Configuration files for various services.
    • database: Migrations, factories, and seeds.
    • public: Entry point for web requests.
    • resources: CSS, JavaScript, and Blade views.
    • routes: Define application routes.
    • storage: Stores files generated by the framework.
    • tests: Unit tests.
    • vendor: Third-party packages.
    • .env: Configuration file for environment variables.
  • Artisan Commands: Useful for various tasks such as creating controllers, models, and migrations.
  • Bootstrap Folder: Configures the applicationā€™s bootstrapping process.

Configuration

  • Publishing Configuration Files: php artisan vendor:publish.
  • Env Variables: Modify settings like database credentials, mail configuration, etc.
  • Compact Function: Simplifies passing data to views.

Middleware

  • Purpose: Acts as a filter for HTTP requests entering your application.
  • Common Middleware: auth, verify, etc.

CRUD Operations

  • Models and Migrations:
    • php artisan make:model Note -m: Creates model and migration.
    • Eloquent ORM for database interactions.
  • Controllers:
    • Resource Controllers: php artisan make:controller NoteController --resource --model=Note
  • Views:
    • Blade templates: resources/views/note/*.blade.php
    • Layouts: Separate layout files for unified design.
  • Routes:
    • Resource routes: Simplified declaration using Route::resource('notes', NoteController::class);
    • Manual route declaration for specific actions.

Frontend Integration

  • Vite: Module bundler for integrating frontend assets (CSS, JS).
  • Tailwind CSS: Utility-first CSS framework.
  • CSRF Protection: Prevent cross-site request forgery.
  • Pagination: Implementing paginated data views.
  • Custom Error Pages: Handling specific HTTP status codes.

User Authentication and Management

  • Laravel Breeze: Quick starter kit for authentication (login, registration, password recovery).
    • Installation: composer require laravel/breeze --dev, php artisan breeze:install
  • Email Verification: Ensuring user emails are verified using built-in Laravel features.
  • Session Management: Displaying success/error messages via sessions.

Deployment

  • Hosting: Steps to deploy on shared or VPS hosting.
  • Configuration: Adjust settings for production environment.
  • CI/CD: Automate deployment via GitHub Actions.

Conclusion

  • Future Course: A more comprehensive Laravel course with projects, quizzes, and challenges.

Additional Resources

  • Hostinger Web Hosting: Recommendation for affordable and fast hosting services.
  • Extensions: Useful extensions for VS Code for Laravel development.