Project Structure
Directory Structure
Organize your Laravel project using the standard directory structure for maintainability and scalability.
app/
Console/ # Artisan commands
Exceptions/ # Custom exception classes
Http/
Controllers/ # Application controllers
Middleware/ # HTTP middleware
Models/ # Eloquent models
config/ # Configuration files
database/
migrations/
seeders/
public/
resources/
views/
routes/
web.php
api.php
tests/
vendor/
- Place business logic in appropriate folders (e.g., services in
app/Services). - Use
resources/viewsfor Blade templates and UI components. - Store migrations and seeders in
database/. - Use
routes/for route definitions (web.phpfor web,api.phpfor APIs). - Place tests in the
tests/directory, organized by type (Unit, Feature). - Use
public/for publicly accessible assets (images, JS, CSS). - Use
vendor/for Composer dependencies.
Namespaces
Use PSR-4 autoloading and namespaces to organize code and avoid naming collisions.
// Good
namespace App\Http\Controllers;
use App\Models\User;
// Bad
namespace App\Http\Controllers;
include_once '../../Models/User.php';
- Match namespaces to directory structure (e.g.,
App\Http\Controllersforapp/Http/Controllers). - Use
usestatements for importing classes and interfaces. - Avoid using global functions or classes without namespaces.
Best Practices
- Keep the root directory clean; avoid placing custom code in the root.
- Group related files and features together.
- Use Composer for dependency management and autoloading.
- Document custom directories and their purpose for new team members.
- Regularly review and refactor project structure as the application grows.