Laravel 11 Application Configuration

Marickian
By -
0
Laravel 11 Application Configuration

Laravel 11 Application Configuration

Learn how to configure your Laravel 11 application using the new Application::configure method for a cleaner and more structured setup.

Introduction

Laravel 11 introduces a new way to configure applications using the Application::configure method. This approach simplifies the setup process and makes it easier to manage routing, middleware, exception handling, and more.

Basic Configuration

The Application::configure method is the foundation for setting up your Laravel application. It allows you to define routing, middleware, and exception handling in a structured way.

Example of Basic Configuration


use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))  // Set the base path
    ->withRouting(
        web: __DIR__.'/../routes/web.php',  // Define web routes
        api: __DIR__.'/../routes/api.php',  // Define API routes
        commands: __DIR__.'/../routes/console.php',  // Define console routes
        health: '/up',  // Define health check route
    )
    ->withMiddleware(function (Middleware $middleware) {  // Middleware configuration
        $middleware->add(SomeMiddleware::class);  // Add middleware
    })
    ->withExceptions(function (Exceptions $exceptions) {  // Exception handling
        $exceptions->handle(SomeException::class, SomeHandler::class);  // Handle exceptions
    })
    ->create();  // Create the application instance
        

In this example:

  • basePath: Sets the base path of the application.
  • withRouting: Defines routes for web, API, console, and health checks.
  • withMiddleware: Configures middleware for the application.
  • withExceptions: Configures exception handling.
  • create: Creates the application instance.

Additional Configuration Options

Laravel 11 provides several other configuration options to fine-tune your application. Below are some examples:

Service Providers

You can register custom service providers using the withProviders method.


return Application::configure(basePath: dirname(__DIR__))
    ->withProviders([
        SomeServiceProvider::class,  // Register service providers
        AnotherServiceProvider::class,
    ])
    ->create();
        

Database Configuration

Database settings are typically managed in the .env file, but you can also configure them programmatically.


return Application::configure(basePath: dirname(__DIR__))
    ->withDatabase([
        'default' => env('DB_CONNECTION', 'mysql'),
        'connections' => [
            'mysql' => [
                'driver' => 'mysql',
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'unix_socket' => env('DB_SOCKET', ''),
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'strict' => true,
                'engine' => null,
            ],
        ],
    ])
    ->create();
        

Logging Configuration

Logging is configured in config/logging.php. You can customize logging channels and handlers.


return Application::configure(basePath: dirname(__DIR__))
    ->withLogging([
        'default' => env('LOG_CHANNEL', 'stack'),
        'channels' => [
            'stack' => [
                'driver' => 'stack',
                'channels' => ['single', 'daily'],
            ],
            'single' => [
                'driver' => 'single',
                'path' => storage_path('logs/laravel.log'),
                'level' => 'debug',
            ],
            'daily' => [
                'driver' => 'daily',
                'path' => storage_path('logs/laravel.log'),
                'level' => 'debug',
                'days' => 14,
            ],
        ],
    ])
    ->create();
        

Cache Configuration

Cache settings are managed in config/cache.php. You can define different cache stores and their configurations.


return Application::configure(basePath: dirname(__DIR__))
    ->withCache([
        'default' => env('CACHE_DRIVER', 'file'),
        'stores' => [
            'file' => [
                'driver' => 'file',
                'path' => storage_path('framework/cache/data'),
            ],
            'redis' => [
                'driver' => 'redis',
                'connection' => 'default',
            ],
        ],
        'prefix' => 'laravel_cache',
    ])
    ->create();
        

Session Configuration

Session handling is configured in config/session.php. You can customize session drivers, lifetime, and more.


return Application::configure(basePath: dirname(__DIR__))
    ->withSession([
        'driver' => env('SESSION_DRIVER', 'file'),
        'lifetime' => env('SESSION_LIFETIME', 120),
        'expire_on_close' => false,
        'encrypt' => false,
        'files' => storage_path('framework/sessions'),
        'connection' => env('SESSION_CONNECTION', null),
        'table' => 'sessions',
        'store' => env('SESSION_STORE', null),
        'lottery' => [2, 100],
        'cookie' => env(
            'SESSION_COOKIE',
            str_slug(env('APP_NAME', 'laravel'), '_').'_session'
        ),
        'path' => '/',
        'domain' => env('SESSION_DOMAIN', null),
        'secure' => env('SESSION_SECURE_COOKIE', false),
        'http_only' => true,
        'same_site' => 'lax',
    ])
    ->create();
        

Conclusion

Laravel 11's Application::configure method provides a powerful and flexible way to configure your application. By using this method, you can define routing, middleware, exception handling, service providers, database configuration, logging, cache, and session handling in a structured and maintainable way. This approach helps keep your configuration clean and organized, making it easier to manage as your application grows.

Post a Comment

0Comments

Post a Comment (0)