Laravel Tutorial #2: Routing

    More Tutorials on Web Development

    In the second part of this 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:

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

    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

    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>

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

    Also, we can make it return a string:

    Route::get('/', function () {
        return 'Hello, World!';

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

    Refresh the browser,

    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']);

    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 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']);

    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');

    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.

    Leave a Reply

    Your email address will not be published. Required fields are marked *