Catalog / Laravel Lumen Cheat Sheet
Laravel Lumen Cheat Sheet
A concise cheat sheet for Laravel Lumen, covering essential commands, configurations, and functionalities to help you build microservices and APIs efficiently.
Getting Started
Installation
Install Lumen via Composer:
|
Navigate to your project directory:
|
Run the local development server:
|
Basic Configuration
|
Configure environment-specific settings, such as database connections and API keys. Example:
|
|
Configure middleware, service providers, and exception handling.
|
Enable Facades: |
Uncomment
|
Enable Eloquent: |
Uncomment
|
Routing
Define routes in Example:
|
Available HTTP methods: |
Route parameters:
|
Controllers & Middleware
Controllers
Generate a controller:
|
Example Controller:
|
Register the controller route:
|
Middleware
Creating Middleware: |
Generate middleware:
|
Example Middleware: |
|
Registering Middleware: |
Register in
|
Applying Middleware: |
Apply to a route:
|
Request & Response
Accessing Request Data:
|
Returning JSON Responses:
|
Setting Headers:
|
Database & Models
Database Configuration
Configure database settings in
|
Ensure Eloquent is enabled in
|
Eloquent Models
Creating a Model: |
|
Using a Model: |
|
Model Attributes: |
|
Database Migrations
Create a migration:
|
Example migration file:
|
Run migrations:
|
Authentication & Authorization
Authentication
Lumen provides basic authentication scaffolding. Consider using Passport or JWT for more robust authentication. |
Using basic authentication middleware:
|
Authorization (Policies)
Creating a Policy: |
|
Registering a Policy: |
Register the policy in
|
Using a Policy: |
|
Events & Listeners
Define events and listeners to decouple components. Example Event:
|
Example Listener:
|
Register events and listeners in |