Laravel101: How to define routes in your projects
Tags: laravel, php, tutorial
Published: 2023-07-26 10:24:33

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.

{% embed https://dev.to/kazemmdev/laravel-101-everything-you-need-to-know-to-get-started-with-this-popular-php-framework-2gal %}
---

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wukis3rbz0mwakzaoaol.png)


Let’s take a closer look at the code in this file:
```php
Route::get('/', function () {
    return view('welcome');
});
```
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:
```php
Route::post('/', function (Request $request) {
    // handle POST request
});
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nslec57ovh1mxcp3fnfg.png)

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](https://code.visualstudio.com/) you could choose any editor you want like [phpstorm](https://www.jetbrains.com/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!