Laravel’s application structure is designed to provide a clear and organized layout for building web applications. Here, I’ll provide a more detailed explanation of each major directory in Laravel’s application structure, along with examples to illustrate their usage:

Application Structure in Laravel

1. Root Directory:

  • contains the main application entry point as well as additional configuration files.
  • Example files:
    • .env: Configuration settings for the application, such as database credentials
    • composer.json: Dependency management file for Composer, which manages PHP package dependencies.

2. App Directory (app/):

  • The core of your application where most of the business logic resides.
  • Example files and directories:
    • Http/Controllers/: Controllers that handle HTTP requests and define the application’s behavior.
// Example controller
namespace App\Http\Controllers;

class UserController extends Controller {
    public function index() {
        // Your controller action logic here
    }
}
  • Http/Middleware/: Middleware classes for processing requests and responses.
// Example middleware
namespace App\Http\Middleware;

class CheckAdmin {
    public function handle($request, Closure $next) {
        // Middleware logic here
    }
}
  • Http/Requests/: Request classes containing validation rules.
// Example request class
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StorePostRequest extends FormRequest {
    public function authorize() {
        return true;
    }

    public function rules() {
        return [
            'title' => 'required|string|max:255',
            // More validation rules
        ];
    }
}
  • Providers/: Service providers for binding services and components.
  • Console/: Console (CLI) commands and related components.
  • Exceptions/: Custom exception classes.
  • Helpers/: Custom helper functions.

3. Config Directory (config/):

  • Configuration files for various aspects of your application.
  • Example files:
    • database.php: Database configuration settings.
    • cache.php: Configuration for caching.
    • mail.php: Email configuration.

4. Database Directory (database/):

  • contains documents relating to database administration.
  • Example files and directories:
    • migrations/: Database migration files for defining and modifying database tables.
// Example migration file
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {
    public function up() {
        Schema::create('users', function ($table) {
            $table->id();
            $table->string('name');
            // Define table columns
        });
    }

    public function down() {
        Schema::dropIfExists('users');
    }
}
  • factories/: Factory classes for generating fake data during testing.
// Example factory definition
use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory {
    protected $model = User::class;

    public function definition() {
        return [
            'name' => $this->faker->name,
            // Define factory data
        ];
    }
}

5. Public Directory (public/):

  • The web server’s public document root.
  • Example files:
    • The page that receives incoming HTTP requests is called index.php.
    • css/, js/, images/: Publicly accessible assets.

6. Resources Directory (resources/):

  • Contains views, language files, and raw assets.
  • Example files and directories:
    • views/: Blade templates for rendering HTML views.
<!-- Example Blade template -->
<html>
<head>
    <title>{{ $title }}</title>
</head>
<body>
    <p>{{ $content }}</p>
</body>
</html>
  • For localization, use the lang/ language files.
  • assets/: Raw assets like CSS, JavaScript, and images.

7. Routes Directory (routes/):

  • Define routes for handling incoming HTTP requests.
  • Example files:
    • web.php: Routes for web-based interfaces.
    • api.php: Routes for API endpoints.
    • channels.php: Event broadcasting channels.

8. Storage Directory (storage/):

  • Contains application-generated files, such as logs, cached views, and temporary files.

9. Tests Directory (tests/):

  • Holds unit and feature tests for your application.
  • Example files and directories:
    • Unit/: Unit tests.
    • Feature/: Feature tests.

10. Vendor Directory (vendor/):

  • Contains Composer dependencies and autoloaded classes.

Categorized in: