laravel/lumen
Laravel Lumen is a fast PHP micro-framework for building web apps and APIs with elegant syntax. It includes routing, database abstraction, queues, and caching. Note: the Laravel team now recommends starting new projects with Laravel instead.
Installation:
composer create-project laravel/lumen your-project-name
^9.x for Lumen 9.x (latest stable) or ^8.x for older projects.First Use Case:
routes/web.php and define a simple route:
$app->get('/', 'App\Http\Controllers\ExampleController@index');
php artisan make:controller ExampleController
php -S localhost:8000 -t public
http://localhost:8000 to verify.Key Files to Explore:
bootstrap/app.php: Entry point for bootstrapping.config/app.php: Core configuration (providers, aliases).routes/: All route definitions (web.php, api.php).app/Providers/: Service providers (e.g., AppServiceProvider.php).Routing:
Route::get(), Route::post(), etc., or $app->get() for Lumen’s syntax.$app->group(['middleware' => 'auth'], function () {
$app->get('/dashboard', 'DashboardController@index');
});
$app->group(['prefix' => 'api/v1'], function () {
$app->post('/users', 'UserController@store');
});
Dependency Injection:
AppServiceProvider:
$this->app->bind(
App\Contracts\UserRepository::class,
App\Repositories\UserRepository::class
);
public function __construct(App\Contracts\UserRepository $users) {
$this->users = $users;
}
Middleware:
bootstrap/app.php:
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
php artisan make:middleware CheckRole
Database:
$user = App\Models\User::find(1);
.env for database connections:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
Validation:
$validator = Validator::make($request->all(), [
'email' => 'required|email',
]);
Testing:
tests/Feature:
public function test_example_route()
{
$response = $this->get('/');
$response->assertStatus(200);
}
phpunit
laravel/sanctum, spatie/laravel-permission).
bootstrap/app.php.php artisan queue:work (configure QUEUE_CONNECTION in .env).Cache::put() or Cache::remember() with drivers like Redis or file.app/Events and listeners in app/Listeners.Middleware Differences:
VerifyCsrfToken middleware is not auto-registered (unlike Laravel).bootstrap/app.php:
$app->routeMiddleware([
'csrf' => \App\Http\Middleware\VerifyCsrfToken::class,
]);
Service Provider Booting:
config/app.php like Laravel.bootstrap/app.php:
$app->withFacades();
$app->withEloquent();
Route Caching:
php artisan route:cache (use php artisan route:list instead).Session Handling:
web middleware group).SESSION_DRIVER=file (or another driver) is set in .env.Blade Templates:
composer require illuminate/view
bootstrap/app.php:
$app->register(\Illuminate\View\ViewServiceProvider::class);
File Structure:
resources/views by default. Create it manually if using Blade.bootstrap/app.php for error handling. Customize by modifying:
$app->error(function (\Throwable $e) {
return response()->json(['error' => $e->getMessage()], 500);
});
Log::error(), Log::info(), etc. (requires illuminate/log).APP_LOG in .env (e.g., APP_LOG=single).Custom Commands:
app/Console/Commands and register in bootstrap/app.php:
$app->command('custom:command', \App\Console\Commands\CustomCommand::class);
php artisan custom:command
API Resources:
php artisan make:resource (requires illuminate/http).return new UserResource($user);
Testing Helpers:
Lumen\Testing\TestCase for custom assertions.public function assertJsonContains($key, $value)
{
$this->seeJson($key, $value);
}
Performance:
$app->configure('app');
APP_DEBUG=false in .env for production..env like Laravel, but ensure variables are loaded in bootstrap/app.php:
$app->configure('app');
fruitcake/laravel-cors for CORS support (not built-in).bootstrap/app.php:
$app->middleware([
\Fruitcake\Cors\HandleCors::class,
]);
How can I help you explore Laravel packages today?