Laravel Tutorial for Beginners #2 - Routes & Views

3 min read 15 hours ago
Published on Dec 27, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you will learn how to work with routes and views in Laravel, a popular PHP framework for building web applications. Understanding routing and views is essential for creating dynamic applications, and this guide will provide you with the foundational knowledge to get started.

Step 1: Setting Up Laravel

  • Ensure you have Laravel installed. If you haven't done so, you can follow the installation instructions from Laravel's official documentation.
  • Create a new Laravel project using the command:
    composer create-project --prefer-dist laravel/laravel projectName
    
  • Navigate to your project directory:
    cd projectName
    
  • Start the development server with:
    php artisan serve
    
  • Open your browser and go to http://localhost:8000 to see the Laravel welcome page.

Step 2: Understanding Routes

  • Open the routes/web.php file. This is where you define routes for your application.
  • A route is a URL pattern that your application responds to. You can create a simple route like this:
    Route::get('/hello', function () {
        return 'Hello, World!';
    });
    
  • Save the file and visit http://localhost:8000/hello in your browser to see the output.

Step 3: Creating a Controller

  • Generate a controller using Artisan command:
    php artisan make:controller HelloController
    
  • Open the newly created controller located in app/Http/Controllers/HelloController.php.
  • Define a method within the controller:
    public function index() {
        return view('hello');
    }
    

Step 4: Creating a View

  • Create a new view file in the resources/views directory named hello.blade.php.
  • Add some HTML content to hello.blade.php:
    <h1>Hello, World!</h1>
    <p>Welcome to my Laravel application!</p>
    
  • Save the file.

Step 5: Updating the Route to Use the Controller

  • Modify your route in routes/web.php to use the newly created controller:
    Route::get('/hello', [HelloController::class, 'index']);
    
  • Refresh your browser at http://localhost:8000/hello to see the view rendered.

Step 6: Passing Data to Views

  • You can pass data from your controller to the view. Update the index method in HelloController:
    public function index() {
        $data = ['message' => 'Welcome to my Laravel application!'];
        return view('hello', $data);
    }
    
  • In your hello.blade.php, display the passed data:
    <p>{{ $message }}</p>
    

Conclusion

In this tutorial, you learned how to set up a Laravel project, define routes, create a controller, and render views. You also discovered how to pass data to views, which is a fundamental concept in Laravel development.

For the next steps, consider exploring more about Laravel's routing features, such as route parameters and middleware, to further enhance your web applications.