DEV Community

Cover image for Laravel101: How to define routes in your projects
Kazem
Kazem

Posted on • Originally published at kazemm.dev

Laravel101: How to define routes in your projects

Welcome to the second article in our Laravel tutorial series! In the previous article, we learned how to install and set up a Laravel project, and how to use the Artisan command-line tool to create a new project.

The point about the serve command is that by closing the terminal, this access environment is closed. So if you need a terminal, don't forget to use another terminal or run the serve command again to restart the access environment.

Routes

In this article, we will focus on routing, which is the process of defining URLs that can be used to access different parts of our application. We will explore the different types of routes that Laravel supports, and how to create and manage routes in our project.
If you enter to your Laravel project, you will see a lot of files and folders there, no worry! we will get to know all these things in this tutorial series.
To start, let's open the routes/web.php file in our project's directory. This file contains the default route that Laravel creates for us, which is a GET request to a view of the page that you just see on previous article.

Image description

Let’s take a closer look at the code in this file:



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


Enter fullscreen mode Exit fullscreen mode

As you can see, this route uses the get method, which specifies the type of request that this route should handle (in this case, a GET request). The first parameter passed to the get method is the URL pattern that this route should match, which in this case is the root URL of our application (/); the second parameter is the callback function that will be executed when this route is matched.

In this example, the callback function returns a view called welcome. Views are a way to define reusable UI components in Laravel, and we will learn more about them in a future article. For now, let's focus on the route itself.

We can create more routes in this file by adding new Route definitions, each of which can handle a different type of request or URL pattern. For example, we can add a new route that handles POST requests to the root URL of our application:



Route::post('/', function (Request $request) {
    // handle POST request
});


Enter fullscreen mode Exit fullscreen mode

We can also define routes that handle different methods (such as GET, POST, PUT, DELETE, etc.) by using the corresponding method instead of get or post in the Route definition. In this way, we can define different routes for different parts of our application, and handle different types of requests in a flexible and modular way.

Next, I want to add another route, for example, the famous about page!

Image description

Here we go! So easily you sent a value from the back side to the front side.

Later we talk more about the view templates and I will give in detail how to add the styles together in the browser pages.

Laravel Code Editor

As you see I’ll use vscode editor you could choose any editor you want like phpstorm, But let me list some of the most useful VS Code extensions that every Laravel developer should consider having on their VS Code:

  • Composer from devsense
  • Laravel Blade Snippets from Winnie Lin
  • Laravel Snippets from Winnie Lin
  • PHP Namespace Resolver from Mehedi Hassan
  • PHP IntelliSense from Damjan Cvetko
  • VS Browser from Phu1237
  • Auto Rename Tag from Jun Han

That’s it! You’ve just created your first routing system using Laravel. Of course, this is just the tip of the iceberg. There are many more features and options you can use to customize your routing system. But for now, this will do.

In the next tutorial, we’ll learn how to create views for our application. Views are like the pretty faces of your application. They’re the parts of your app that users will interact with. So, stay tuned for that!

Top comments (2)

Collapse
 
ahid_technologies_0e0c805 profile image
Ahid Technologies

This series is awesome for newbies.
It would be better If you could add some advanced topics like Events, Tests and Queues etc.

Collapse
 
kazemmdev profile image
Kazem

Thank you @ahid_technologies_0e0c805 I'll add some advanced topics later in future!