(Beginner Laravel) Part 1: Basic Routing

Tram Ho

This is a series of articles in an easy to understand, simple, basic style, suitable for those starting with Laravel from scratch.

Laravel Routing

This section will cover the following:

  • How to define a route.
  • How to pass route parameters to the view
  • Respond to a request with the controller’s handler function.
  • A little introduction to php artisan

How to define a route – Basic Routing and Views

In a laravel project, the routes directory is the directory where you can define all the paths for your website. The basic syntax is as follows:

When we request to the web with the get method and the path is / (corresponding to the homepage), the function declared inside will be executed. Normally, inside the function, a corresponding view will be returned. Laravel will check the views in the resources / views directory and return the view we requested (if any).

Here we can customize HTTP methods, path path, function inner handler and return object (can be string, view, json, variable, …)

How to pass parameters in the route to the view – Pass Request Data to Views

We can get the data from the request through the query string using the helper function request.

For example, the link is we can get the data as follows:

And to have that variable take effect in the view, we can program it as follows:

Alternatively, let’s consider a way to reduce the code like (refractoring):

In the view there are ways to call the following variables:

We will have some notes here:

  • The first two ways will purely print out the contents of the variable. Therefore, the user can pass a script and of course when the request comes to the corresponding link, the script will be executed immediately. This is quite dangerous and violates security regulations. Therefore, the recommended way would be method 3. This command line processing will escape the passed-in code.
  • Method 2 and 3 is the pure command line of laravel, php will not understand this syntax. But laravel will automatically render the corresponding syntax, which is suitable for php. You can check the corresponding php command in the storage/framework/views .

Route Wildcards

We continue to look at the next example on how to use routing. Let’s take a look at the following code:

Responding to a request using the controller handler function.

When responding to a request instead of a function, we can lead to a controller with the corresponding method. Gradually, we can see how the MVC pattern is applied in laravel. How to start the method in the controller corresponding to the request:

The following code will have the same handling in the Route Wildcards code:

About php artisan

In addition to the php artisan serve command that is very familiar to everyone, php artisan has many other uses. You can type php artisan code to see what this tool can do.

The application in this section is the controller generation syntax with the command: php artisan make:controller InfosController

This part is here to be concluded! Reference source: https://laracasts.com/series/laravel-6-from-scratch/

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo