How Laravel works

Learn how Laravel works with an example of creating a simple blog app to get started.

Laravel is a free, open source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern. Laravel provides an expressive, elegant syntax for working with databases and the robust tools needed for large, robust applications.

Routing

Routing is the process of taking a URI and that maps it to a closure or controller. Routes can be defined in the routes/web.php file. A basic example of a route is shown below:


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

The code above defines a route that responds to a GET request to the root of the application. The closure will return the welcome view. The route can also be mapped to a controller. The code below shows an example of a route mapped to a controller:


Route::get('/', 'HomeController@index');

In this example, the route is mapped to the HomeController controller and the index method. When the route is accessed, the controller method will be executed. Laravel also has the ability to define routes with parameters. The code below shows an example of a route with a parameter:


Route::get('user/{id}', 'UserController@show');

In this example, the route is mapped to the UserController controller and the show method. The route also contains a parameter, {id}, which is used to pass an ID to the controller method. The controller method can then use the ID to retrieve the appropriate user from the database.

Controllers

Controllers are classes that handle HTTP requests. They can be used to group related request handling logic into a single class. Controllers can also contain request middleware, which can be used to filter HTTP requests before they reach the controller. The code below shows an example of a controller:


class HomeController extends Controller
{
    public function index()
    {
        return view('welcome');
    }
}

In this example, the HomeController class contains a single method, index. This method will return the welcome view when the route is accessed. Controllers can also contain other methods that handle other HTTP requests, such as POST and PUT requests.

Models

Models are classes that are used to interact with the database. They can be used to query the database, insert data into the database, update data in the database, and delete data from the database. Models can also contain methods for validating data before it is inserted into the database. The code below shows an example of a model:


class User extends Model
{
    public function validate($data)
    {
        // Validation logic
    }
}

In this example, the User class is a model. It contains a validate method that can be used to validate data before it is inserted into the database. Models can also contain methods to query the database, insert data into the database, update data in the database, and delete data from the database.

Views

Views are files that contain HTML and other markup for displaying data in a web page. Views can also contain logic for displaying data, such as conditionals and loops. The code below shows an example of a view:


<h1>Welcome</h1>

<p>This is the welcome view.</p>

<ul>
  @foreach ($users as $user)
    <li>{{ $user->name }}</li>
  @endforeach
</ul>

In this example, the view is rendering a list of users. The view is using a foreach loop to loop through the list of users and display their names. Views can also contain other logic, such as conditionals and loops.

Conclusion

Laravel is a powerful, robust web development framework. It provides an expressive, elegant syntax for working with databases and the tools needed for large, robust applications. It also provides a routing system, controllers, models, and views for building complex web applications.

Answers (0)