Laravel Routing
In Laravel, all requests are mapped with
the help of routes. Basic routing routes the request to the associated
controllers. This chapter discusses routing in Laravel.
Routing in Laravel includes the following
categories:
· Basic
Routing
· Route
parameters
· Named
Routes
· Basic
Routing
All Laravel routes are defined in your
route files, which are located in the routes directory. These files are
automatically loaded by your application's App\Providers\RouteServiceProvider.
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.
The sample route for the welcome page can be seen as shown in the screenshot given below:
The routing mechanism is shown in the image given below
Let us now
understand the steps involved in the routing mechanism in detail:
1. Step 1: Initially,
we should execute the root URL of the application.
2. Step 2: Now, the
executed URL should match with the appropriate method in the route.php file. In
the present case, it should match the method and the root (‘/’) URL. This will
execute the related function.
3. Step 3: The
function calls the template file resources/views/welcome.blade.php.
Next, the function calls the view() function with the argument ‘welcome’
without using the blade.php.
This will produce the HTML output as shown in the image below
Sometimes in the web application, you may
need to capture the parameters passed with the URL. For this, you should modify
the code in routes.php file.
You can capture the parameters in
routes.php file in two ways as discussed here:
· Required
Parameters
These parameters are those which should be mandatorily captured for routing the web application. For example, it is important to capture the user’s identification number from the URL. This can be possible by defining route parameters as shown below:
· Optional
Parameters
Named routes allow a convenient way of creating routes. The chaining of routes can be specified using the name method onto the route definition. The following code shows an example for creating named routes with a controller:
The user controller will call for the
function showProfile with parameter as a profile. The parameters use the name
method in the route definition.
For more resources check: Laravel.com
0 Response to "Laravel Routing"
Post a Comment