php

Laravel Route

The route is used to create a request URL for the Laravel application. The URL is defined in the route file in a human-readable format. In Laravel 7, all types of route information are stored in two files, web.php and api.php. These files are located in the routes folder of the Laravel project. All web application-related routes are defined in web.php and all API-related routes are defined in api.php. This tutorial covers different types of routing methods and how the get() method can be used for defining the different routes for Laravel projects.

Route Methods

Some common route methods used in Laravel to handle HTTP requests are explained below.

A. Route::get($uri, $callback_function)

The basic Laravel route, mainly used to display static pages.

B. Route::post($uri, $callback_function)

Used to create any new item.

C. Route::put($uri, $callback_function)

Used to update or replace database record.

D. Route::patch($uri, $callback_function)

Used to update or modify database record.

E. Route::delete($uri, $callback_function)

Used to delete database record.

F. Route::any($URI, $callback)

Used to handle all types of HTTP requests.

Uses of the get() Route Method

Default Route

When you create a new Laravel project, the following default route is found by default in the web.php file. This displays the content of the welcome view file for the base URL of the project.

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

Run the base URL of the Laravel project from the browser.

http://localhost/laravelpro/public/

The following output will appear.

If you change the output of the base URL with the following route, then it will display the simple text “Welcome to LinuxHint” in the browser.

Route::get('/', function () {
    return 'Welcome to LinuxHint';
});

Again, run the base URL to check the output.

Basic get() Route

The following route will iterate a for loop 5 times and print the square values of the numbers from 1 to 5.

Route::get('square', function () {
    for($i =1; $i <= 5; $i++){
        echo "The square of $i = ".pow($i,2)."<br>";
    }
});

Run the following URL from the browser.

http://localhost/laravelpro/public/square

The following output will appear.

Route Using Route Parameter

You can use the parameter with the request URL to pass as the function argument. In the following route, two parameters are used that are passed in the function as $x and $n. Here, $x is used as a base and $n is used as an exponent. $x to the power $n will be printed in the browser after executing the route.

Route::get('power/{x}/{n}', function ($x,$n) {
    echo "$x to the power $n = ".pow($x,$n)."<br>";
});

Run the following URL from the browser. Here, the base value is 3 and the exponent value is 4.

http://localhost/laravelpro/public/power/3/4

The following output will appear.

Route Using the Optional Parameter

You can use the optional route parameter by using the ‘?’ symbol. This means that if you pass the route parameter value, then it will be used in the function argument, and if the parameter is omitted, then the default value of the function argument will be used. If the parameter value is more than 99, then it will print the message: “The number contains more than 2 digits.” If the value is more than 9, then it will print the message: “The number contains 2 digits.” If the parameter value is less than 9 or omitted, then it will print the message: “The number contains 1 digit.”

Route::get('check/{number?}', function ($number = 0) {
    if($number > 99)
        return "The number contains more than 2 digits";
    else if($number >9)
        return "The number contains 2 digits";
    else
        return "The number contains 1 digit";
});

Run the following URL from the browser. Here, 120 is given as the number value.

http://localhost/laravelpro/public/check/120

The following output will appear.

If you omit the number value from the URL, then the following output will appear.

Route with a Regular Expression

You can use any regular expression pattern to validate the route parameter value. In the following route, the phone parameter is validated using the pattern, ‘^0[0-9]{10}.’ The pattern indicates that the value of the phone will start at 0 and will contain any other 10 digits.

Route::get('customer/{phone}', function ($phone) {
    echo "Phone number is $phone";
})->where('phone', '^0[0-9]{10}');

Run the following URL from the browser. Here, ‘01916074567’ is given as the value of the phone parameter.

http://localhost/laravelpro/public/customer/01916074567

The following output will appear.

Routes for Controller

Run the following command to create a controller named BookController.

$ php artisan make:controller BookController

Add the following index() method inside the controller to print the details of a book.

public function index()
{
    echo "Book Name: Beginning Laravel<br/>";
    echo "Author Name: Sanjib Sinha <br/>";
    echo "Publication: Apress<br/>";
    echo "Price: $35";
}

A. Simple route for controller

Now, add the following route in the web.php file to call the index() method of BookController.

Route::get('book', 'BookController@index');

Run the following URL from the browser.

http://localhost/laravelpro/public/book

The following output will appear.

B. Named route for controller

The named route is used to provide an alternative name to a route, which allows you to redirect the route to a particular route. Add the following line at the end of the index() method of BookController to create a hyperlink.

echo "<br/><a href ='".route('other')."'>Next Book</a>";

Add the anotherBook() method with the following code inside the Bookcontroller to access this method using the named route.

public function anotherBook()
{
    echo "book name: 'Laravel 5 Essentials'<br/>";
    echo "Author Name: 'Martin Bean' <br/>";
    echo "Price: $30<br/>";
    echo "Publication: PAKCT<br/>";
}

Now, add the following named route in the web.php file.

Route::get('book/other', [
'as' => 'other', 'uses' => 'BookController@anotherBook'
]);

Run the following URL again from the browser and click on the Next Book link.

http://localhost/laravelpro/public/book

The following output will appear after clicking the link.

Conclusion

This tutorial covered the various uses of the get() method in routing to clarify the concept of this method in Laravel routing. In this article, basic routing, routing with parameters, routing with the regular expression, and routing with the controller were explained through various examples. I hope that this tutorial has helped you understand the routing basics of Laravel.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.