Back to results for “Architecture”
PHP › Namespaces & Autoloading

How do you organize namespaces effectively in a larger application to reflect logical groupings such as models, services, and controllers?

Intermediate

Answer

A common and effective convention organizes namespaces to mirror an application's logical architecture, such as placing all data model classes under an App\Models namespace, business logic service classes under App\Services, and request handling controllers under App\Http\Controllers, directly corresponding to a matching folder structure on disk, which makes it immediately intuitive for any developer to predict exactly where a specific type of class should live simply based on its responsibility.
namespace App\Http\Controllers;
namespace App\Models;
namespace App\Services;
namespace App\Repositories;
Real-world example A growing Laravel application organizes its custom service classes under a clearly named App\Services namespace, making it immediately obvious to any developer joining the team exactly where to look for or add business logic that does not naturally belong on a specific Eloquent model.

Common follow-ups: How do most popular PHP frameworks structure their default namespace organization?;What happens when an application's namespace organization no longer matches its actual growing complexity?