Architecture

Laravel is built on the Model-View-Controller (MVC) architectural pattern, which separates the application into three main components: the model, the view, and the controller.

Model

The model is responsible for managing the data of the application. It is typically used to retrieve, insert, and update data in a database. In Laravel, models are usually stored in the app/Models directory.

Here is an example of a simple model class in Laravel:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'users';
}

 

View

The view is the front-end component of the MVC pattern, which is responsible for displaying the user interface. In Laravel, views are usually stored in the resources/views directory and use PHP or Blade templates.

Blade is a simple, yet powerful templating engine provided with Laravel. It allows you to use PHP code in your views and extends the functionality of plain PHP templates by providing additional directives, such as @if, @foreach, and @include.

Here is an example of a simple view file in Laravel using Blade syntax:

<!DOCTYPE html>
<html>
    <head>
        <title>My Laravel App</title>
    </head>
    <body>
        <h1>Hello, {{ $name }}!</h1>
    </body>
</html>

 

Controller

The controller is the middleman between the model and the view. It receives requests from the user, retrieves data from the model, and passes it to the view for rendering. In Laravel, controllers are usually stored in the app/Http/Controllers directory.

Here is an example of a simple controller class in Laravel:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function show(Request $request, $id)
    {
        $user = User::find($id);
        return view('users.show', ['user' => $user]);
    }
}

 

Request Lifecycle

In Laravel, every HTTP request follows a defined lifecycle. When a request is made, it is first received by the router, which determines which controller and action should be called to handle the request.

The request is then passed to the appropriate controller, which retrieves the necessary data from the model and passes it to the view for rendering. The view is then returned to the browser as a response.

 

Service Container Binding and Resolution

Laravel's service container is a powerful tool for managing class dependencies and performing dependency injection. It is responsible for creating and resolving class instances, which are referred to as bindings.

You can bind an interface to a concrete implementation using the bind method in the service container:

$this->app->bind(
    'App\Contracts\UserRepositoryInterface', 'App\Repositories\DbUserRepository'
);

You can also use the singleton method to bind a class or interface to a shared instance:

$this->app->singleton(
    'App\Services\Twitter',
    function () {
        return new \App\Services\Twitter('api-key');
    }
);

To resolve a class instance from the service container, you can use the make method:

$twitter = $this->app->make('App\Services\Twitter');

 

Service Providers

Service providers are the central place to bind classes to the service container and bootstrap your application. All Laravel core services are registered in service providers.

You can create your own service providers by extending the Illuminate\Support\ServiceProvider class and overriding the register and boot methods:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        //
    }

    public function boot()
    {
        //
    }
}

 

You can then register your service provider in the config/app.php configuration file:

'providers' => [
    // Other service providers...
    App\Providers\AppServiceProvider::class,
],

 

Facades

Facades are a way to provide a static interface to classes that are available in the service container. They are defined as a class that extends the Illuminate\Support\Facades\Facade base class and has a static accessor method that returns the resolved object from the service container.

Here is an example of a simple facade class:

<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Twitter extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'twitter';
    }
}

You can then use the facade like this:

use App\Facades\Twitter;

Twitter::getTimeline();

 

HTTP Verbs

Laravel provides several ways to specify the HTTP verb (GET, POST, PUT, PATCH, DELETE) for a route.

You can use the get, post, put, patch, and delete methods on the Route facade to specify the verb for a route:

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

Route::post('/', function () {
    //
});

Route::put('/', function () {
    //
});

Route::patch('/', function () {
    //
});

Route::delete('/', function () {
    //
});

 

You can also use the match method to specify multiple verbs for a route:

Route::match(['get', 'post'], '/', function () {
    //
});

 

Alternatively, you can use the any method to register a route that responds to any HTTP verb:

Route::any('/', function () {
    //
});

 

You can also specify the HTTP verb in the route parameters as an array:

Route::post(['/', 'uses' => 'HomeController@index']);

 

Finally, you can use the Route::resource method to create a route for a resourceful controller, which will automatically create routes for the following verbs: GET, HEAD, POST, PUT, PATCH, and DELETE.

Route::resource('photos', 'PhotoController');

 

This will create the following routes:

Verb URI Action Route Name
GET /photos index photos.index
GET /photos/create create photos.create
POST /photos store photos.store
GET /photos/{photo} show photos.show
GET /photos/{photo}/edit edit photos.edit
PUT/PATCH /photos/{photo} update photos.update
DELETE /photos/{photo} destroy photos.destroy

I hope this gives you a better understanding of the architecture and key concepts of Laravel. Let me know if you have any questions!