loading...
Cover image for Laravel 8 Tutorial #2: Routing

Laravel 8 Tutorial #2: Routing

ericnanhu profile image Eric Hu Originally published at techjblog.com Updated on ・3 min read

You can download the source code of this tutorial here: https://www.techjblog.com/index.php/laravel-tutorial-for-beginners/

In the second part of our tutorial, we’ll take a look at the routing in Laravel, which is the entry point of our project.

Basic Routing

The most basic router in Laravel accepts a URL and then returns a value. The value could be a string, a view or a controller. All the routes are stored in routes/web.php:

web.php<?php

Route::get('/', function () {
    return view('welcome');
});
Enter fullscreen mode Exit fullscreen mode

This piece of code means when the Laravel router receives “/“, it returns a view called “welcome“, which is located at resources/views/welcome.blade.php.

Open the browser, go to http://localhost, you will get this

img

To make sure this is the view that was returned, we make some changes to the welcome.blade.php by adding the following code somewhere in the file.

<h1 style="color: white">Test</h1>
Enter fullscreen mode Exit fullscreen mode

Refresh the browser and we can see the change we just made,

img

Also, we can make it return a string:

web.phpRoute::get('/', function () {
    return 'Hello, World!';
});
Enter fullscreen mode Exit fullscreen mode

When the Laravel router receives “/“, instead of a view, it returns the string “Hello, World!“,

Refresh the browser,

img

Or we can make it return a controller, which points to a view. We’ll talk about controllers in detail in the next article.

Route::get('/user', [UserController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode

This line of code means if the router receives “/user”, Laravel will go to the UserController, and invoke the index method. Remember, this is the new syntax in Laravel 8, the old syntax will not work here!

Route Parameters

Sometimes you need to use segments in a URL as parameters. For example, imagine we have a fully developed blog app and imagine there is a user who is looking for a blog post with the slug test-post, and he is trying to find that post by putting http://www.myblog.com/post/test-post in his browser.

To make sure that the user finds the correct post, we need to take the last part of that URL, and send it to the backend as a parameter. After that, our controller can use that parameter to find the correct post and return it back to the user.

To do that, we write the following code:

Route::get('post/{slug}', [PostController::class, 'show']);
Enter fullscreen mode Exit fullscreen mode

This will make sure the Laravel router treats everything after post/ as a parameter, and give it a name slug when Laravel sends it to the backend. Route parameters are always encased within {} braces and should consist of alphabetic characters, and may not contain a - character.

Named Routes

Named routes allow the convenient generation of URLs or redirects for specific routes. You may specify a name for a route by chaining the name method onto the route definition.

Route::get('user/profile', [UserController::class, 'show'])->name('profile');
Enter fullscreen mode Exit fullscreen mode

And now, when we need to access this URL, all we need to do is invoke the function route('profile').

Of course, this is only an elementary introduction about Laravel routing. There is an entire section on routing in the official documentation if you are interested.

In the next article, we’ll investigate the MVC structure, which is commonly used by most web development frameworks today.

Related Articles

How to Make Your Server More Secure

Laravel Tutorial For Beginners

Django Tutorial For Beginners

Build A Unit Converter with Vue.js

Discussion

pic
Editor guide