Now before we get started with creating our first Laravel Project, let’s discuss the basic to have an understanding of the framework.
Routing is one of the essential concepts in Laravel. Routing in Laravel allows you to route all your application requests to its appropriate controller. The main and primary routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure, given that it should have to be a simple and expressive way of routing. In this blog, you will learn about the routing concept of Laravel.
Create Routes in Laravel
All the routes in Laravel are defined within the route files that you can find in the routes sub-directory. These route files get loaded and generated automatically by Laravel framework. The application’s route file gets defined in app/Http/routes.php file. The general routing in Laravel for each of the possible request looks something like this:
The Default Route Files
All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group.
For most applications, you will begin by defining routes in your routes/web.php file. The routes defined in routes/web.php may be accessed by entering the defined route’s URL in your browser. For example, you may access the following route by navigating to http://your-app.test/user in your browser:
The Routing Mechanism In Laravel
The routing mechanism takes place in three different steps:
- First of all, you have to create and run the root URL of your project.
- The URL you run needs to be matched exactly with your method defined in the root.php file, and it will execute all related functions.
- The function invokes the template files. It then calls the view() function with the file name located in resources/views/, and eliminates the file extension blade.php at the time of calling.
In many cases, within your application, a situation arises when you had to capture the parameters send ahead through the URL. For using these passed parameters effectively, in Laravel, you have to change the routes.php code.
- Required parameter
- Optional Parameter
There are many parameters which do not remain present within the URL, but the developers had to use them. So such parameters get indicated by a ? (question mark sign) following the name of the parameter.
Available Router Methods
The router allows you to register routes that respond to any HTTP verb:
If you are defining a route that redirects to another URI, you may use the Route::redirect method. This method provides a convenient shortcut so that you do not have to define a full route or controller for performing a simple redirect:
By default, Route::redirect returns a 302 status code. You may customize the status code using the optional third parameter:
You may use the Route::permanentRedirect method to return a 301 status code: