Lesson 2
Basic Laravel App with a Controller and View
Basic Laravel App with a Controller and View

Welcome to the next step in your Laravel journey! In this lesson, we will delve into two crucial components of the Laravel framework: Controllers and Views. Building on the knowledge from the previous lesson, where you learned about routing and views, this lesson will introduce how a controller can be used to manage and process application logic while working seamlessly with views to present data.

What You'll Learn

In this section, we'll explore what a Controller is in the context of a Laravel application and understand how it fits into the Model-View-Controller (MVC) architecture. You'll learn how to create a controller and connect it with a view.

Here's a sneak peek of how a basic controller routes and displays messages on a webpage:

app/routes/web.php

php
1<?php 2 3use Illuminate\Support\Facades\Route; 4use App\Http\Controllers\WelcomeController; 5 6Route::get('/', [WelcomeController::class, 'index']);

app/app/Http/Controllers/WelcomeController.php

php
1<?php 2 3namespace App\Http\Controllers; 4 5class WelcomeController extends Controller 6{ 7 public function index() 8 { 9 $message = 'Welcome to the world of controllers!'; 10 return view('welcome', ['message' => $message]); 11 } 12}

app/resources/views/welcome.blade.php

HTML, XML
1<!DOCTYPE html> 2<html> 3<head> 4 <title>Welcome</title> 5</head> 6<body> 7 <h1>{{ $message }}</h1> 8</body> 9</html>

In this example, you see how a controller method called index returns a view with a message, demonstrating the interaction between a Controller and a View.

Let's dive into the details and explore how the application changes when you introduce a controller:

  • In the routes/web.php file, you define a route that points to the index method of the WelcomeController class using the Route::get method with the URL /.
  • The WelcomeController class is created in the app/Http/Controllers directory. It extends the base Controller class and contains an index method that returns a view called welcome with a message. Remember the view() helper function that we used in the previous lesson? It's used here to render the view, the only difference is that we're calling it from a controller method instead of directly from the route.
  • The welcome.blade.php view file is stored in the resources/views directory. It contains a simple HTML structure with a message variable that displays the message passed from the controller. Remember the {{ $message }} syntax? It's used to display the message variable in the view. In the controller, the view() function passes data to the view by using an associative array. In the example, ['message' => $message] means the key 'message' will be available in the view as a variable ($message). Blade's double curly braces {{ $message }} safely display this variable’s content in the HTML.

By following these steps, you can create a basic Laravel application with a controller and view. This setup allows you to separate the application logic from the presentation layer, making your code more organized and maintainable.

Why It Matters

Controllers play a crucial role in Laravel applications. They act as intermediaries between the routes and the views, handling the application logic and processing data. Here are some key reasons why controllers are essential:

  1. Separation of Concerns: Controllers help separate the application's concerns by keeping the logic separate from the presentation layer. This separation makes the codebase more organized and easier to maintain.
  2. Reusability: Controllers allow you to reuse logic across multiple routes and views. By encapsulating common functionality in controllers, you can avoid duplicating code and promote code reuse.
  3. Testability: Controllers make it easier to test your application logic in isolation. By testing controllers independently of the routes and views, you can ensure that your application behaves as expected.

As you understand how controllers fit into the bigger picture, you'll see how they streamline the development process, encouraging code reuse and separation of concerns. This knowledge will kickstart your path to building more sophisticated Laravel applications with clarity and confidence.

Ready to dive deeper? Let's move on to the practice section and fortify these concepts with hands-on experience.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.