Laravel Deep Dive: Project Structure, API, and Core Concepts

Objectives: Laravel Deep Dive: Project Structure, API, and Core Concepts

Laravel Deep Dive: Project Structure, API, and Core Concepts

Laravel Deep Dive: Project Structure, API, and Core Concepts

1. Laravel Project Structure

After creating a Laravel project with composer create-project laravel/laravel myproject, you get this main structure:

myproject/
β”œβ”€β”€ app/
β”œβ”€β”€ bootstrap/
β”œβ”€β”€ config/
β”œβ”€β”€ database/
β”œβ”€β”€ public/
β”œβ”€β”€ resources/
β”œβ”€β”€ routes/
β”œβ”€β”€ storage/
β”œβ”€β”€ tests/
β”œβ”€β”€ vendor/
β”œβ”€β”€ artisan
β”œβ”€β”€ composer.json
β”œβ”€β”€ package.json

Each folder has a specific purpose:

  • app/ β†’ Contains your Models, Controllers, and business logic.
  • bootstrap/ β†’ Laravel initialization, sets up the application instance.
  • config/ β†’ Configuration files for database, app name, caching, mail, etc.
  • database/ β†’ Migrations (table structures), seeders (sample data), factories.
  • public/ β†’ Entry point index.php and assets (CSS, JS, images).
  • resources/ β†’ Blade templates (views), JS, CSS, and language files.
  • routes/ β†’ Web & API routes (web.php, api.php).
  • storage/ β†’ Logs, cache, and file uploads.
  • vendor/ β†’ Laravel framework and third-party libraries installed via Composer.
  • .env β†’ Environment configuration (database, API keys, app URL).

2. Basic MVC Concept in Laravel

Laravel uses MVC (Model-View-Controller):

  • Model β†’ Represents database tables. Laravel internally uses Illuminate\Database\Eloquent\Model which provides CRUD operations.
  • Controller β†’ Handles request logic, fetches data via Models, and passes it to Views.
  • View β†’ Blade templates render HTML to the browser. Blade allows loops, conditions, and template inheritance.

Example Model (app/Models/Note.php)

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Note extends Model {
    use HasFactory;
    protected $fillable = ['title','content','subject_id'];
}

Example Controller (app/Http/Controllers/NoteController.php)

namespace App\Http\Controllers;

use App\Models\Note;

class NoteController extends Controller {
    // List all notes
    public function index() {
        $notes = Note::all(); // Eloquent retrieves all rows
        return view('notes.index', compact('notes'));
    }

    // Show single note
    public function show($id) {
        $note = Note::findOrFail($id); // Throws 404 if not found
        return view('notes.show', compact('note'));
    }
}

Blade View Example (resources/views/notes/index.blade.php)

@extends('layouts.app')

@section('content')

All Notes

@endsection

3. Routes (routes/web.php)

Routes map URLs to Controllers:

use App\Http\Controllers\NoteController;

Route::get('/', function() {
    return view('welcome');
});

Route::get('/notes', [NoteController::class, 'index']);
Route::get('/notes/{id}', [NoteController::class, 'show']);

Laravel internally uses Illuminate\Routing\Router to match URLs with routes and dispatch the correct Controller method.

4. API Setup (routes/api.php)

Laravel makes API creation simple:

use App\Http\Controllers\Api\NoteApiController;

Route::get('/notes', [NoteApiController::class, 'index']);
Route::get('/notes/{id}', [NoteApiController::class, 'show']);

Example API Controller (app/Http/Controllers/Api/NoteApiController.php)

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Note;
use Illuminate\Http\Request;

class NoteApiController extends Controller {
    public function index() {
        $notes = Note::all();
        return response()->json($notes);
    }

    public function show($id) {
        $note = Note::findOrFail($id);
        return response()->json($note);
    }
}

This shows how Laravel automatically serializes Models to JSON using Illuminate\Http\JsonResponse.

5. Configuration Files and Settings

Key config files:

  • config/app.php β†’ App name, timezone, locale
  • config/database.php β†’ Database connections (MySQL, SQLite, PostgreSQL)
  • config/mail.php β†’ Email settings
  • config/filesystems.php β†’ Storage configuration (local, s3)

Example .env File

APP_NAME=SmartNote
APP_ENV=local
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxx
APP_DEBUG=true
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=smartnote
DB_USERNAME=root
DB_PASSWORD=

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=hello@example.com
MAIL_FROM_NAME="${APP_NAME}"

6. Unique Laravel Concepts

  • Eloquent ORM β†’ Makes database operations intuitive. Example: Note::create(['title'=>'Test','content'=>'abc'])
  • Blade Templating β†’ Template inheritance with @extends and @section
  • Middleware β†’ Filters HTTP requests (auth, logging, throttle)
  • Artisan CLI β†’ Built-in command line tool for migrations, seeding, cache clearing, generating controllers/models
  • Service Providers β†’ Initialize core services; Laravel internally calls them in bootstrap/app.php
  • Facades β†’ Provide easy access to Laravel services: DB::table('notes')->get(), Cache::put()
  • Jobs & Queues β†’ Asynchronous processing of emails, notifications, and long tasks
  • Policies & Gates β†’ Authorization control per model or resource

7. How Laravel Internally Works

Example: when you call Note::all():

  1. Laravel calls Illuminate\Database\Eloquent\Model::all()
  2. It uses Illuminate\Database\Query\Builder to build SQL query
  3. Query executes via Illuminate\Database\Connection
  4. Result is hydrated into Note model instances
  5. Controller receives data and passes it to Blade template
  6. Blade template compiles into pure PHP echo statements
  7. HTML sent to browser

8. Summary

Laravel integrates:

  • Routes β†’ Mapping URLs
  • Controllers β†’ Request handling
  • Models β†’ Database representation
  • Views β†’ Rendering UI
  • API β†’ JSON responses
  • Config β†’ Environment settings
  • Artisan β†’ Automates tasks
  • Middleware β†’ Request filtering

By following this structure, a beginner can understand not only how to build features but also how Laravel internally powers everything.

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::

➑