Application Structure

Overview

The Forge Framework embraces flexibility in application structure, allowing developers to organize their code in a way that best suits their project's needs. While the framework provides some conventions for consistency, you have the freedom to structure your application using any architectural pattern you prefer.

The only requirements for a Forge application are:

  • A bootstrap file (e.g., MyApp.php) in your application's root directory
  • A config/ directory containing your application's configuration files

Bootstrap Process

The bootstrap file is the entry point of your application. It's responsible for registering your application with the framework, setting up service providers, and configuring the dependency injection container.

namespace MyApp;

use Forge\Core\Contracts\ApplicationContract;
use Forge\Core\DependencyInjection\Container;

class MyApp implements ApplicationContract
{
    public function register(Container $container): void
    {
        // Register your services
        $container->singleton(UserService::class);
        
        // Load configuration
        $config = App::config();
        
        // Set up middleware
        $this->registerMiddleware($config->get('middleware'));
    }

    public function boot(): void
    {
        // Perform any necessary bootstrapping
        $this->loadRoutes();
        $this->registerEventListeners();
    }
}

Design Patterns

Forge allows you to implement any design pattern or architectural style. Here are some common approaches:

MVC Pattern

A traditional MVC structure might look like this:

apps/MyApp/
  ├── Controllers/
  │   ├── UserController.php
  │   └── PostController.php
  ├── Models/
  │   ├── User.php
  │   └── Post.php
  └── Views/
      ├── users/
      └── posts/

Domain-Driven Design

A DDD-style structure might look like this:

apps/MyApp/
  ├── Domain/
  │   ├── User/
  │   │   ├── User.php
  │   │   ├── UserRepository.php
  │   │   └── UserService.php
  │   └── Post/
  │       ├── Post.php
  │       ├── PostRepository.php
  │       └── PostService.php
  ├── Application/
  │   ├── Commands/
  │   └── Queries/
  └── Infrastructure/
      ├── Persistence/
      └── External/

Example Structures

Here are some example application structures that demonstrate Forge's flexibility:

Simple API Structure

apps/MyApp/
  ├── MyApp.php
  ├── config/
  │   ├── app.php
  │   └── api.php
  ├── Controllers/
  ├── Services/
  ├── Repositories/
  └── Models/

Clean Architecture Structure

apps/MyApp/
  ├── MyApp.php
  ├── config/
  ├── Entities/
  ├── UseCases/
  ├── Interfaces/
  │   ├── Controllers/
  │   ├── Presenters/
  │   └── Gateways/
  └── Infrastructure/
      ├── Database/
      └── External/

Remember, these are just examples. You can organize your application's structure in any way that makes sense for your project, as long as you maintain the bootstrap file and configuration directory.