Create Routes in Laravel

Create Routes in Laravel

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 chapter, you will learn about the routing concept of Laravel




Table of Contents
1. Create Routes in Laravel
2. The Routing Mechanism in Laravel
3. Route Parameters
4. Required Parameters
5. Optional Parameter

1. 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 the Laravel framework. The application's route file gets defined in the app/Http/routes.php file. The general routing in Laravel for each of the possible request looks something like this:
Route:: get ('/', function () {
   return 'Welcome to index';
});
Route:: post('user/dashboard', function () {
   return 'Welcome to dashboard';
});
Route:: put('user/add', function () {
//
});
Route:: delete('post/example', function () {
//
});

2. The Routing Mechanism in Laravel

The routing mechanism takes place in three different steps:
  1. First of all, you have to create and run the root URL of your project.
  2. 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.
  3. 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.
Example:
app/Http/routes.php
<?php

Route:: get ('/', function () {
   return view('laravel');
});
resources/view/laravel.blade.php
<html>
<head>
   <title>Laravel5 Tutorial</title>
</head>

<body>
   <h2>Laravel5 Tutorial</h2>
   <p>Welcome to Laravel5 tutorial.</p>
</body>

</html>

3. Route Parameters

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.
Laravel provides two ways of capturing the passed parameter:
  • Required parameter
  • Optional Parameter

4. Required Parameters

At times you had to work with a segment(s) of the URL (Uniform Resource Locator) in your project. Route parameters are encapsulated within {} (curly-braces) with alphabets inside. Let us take an example where you have to capture the ID of the customer or employee from the generated URL.
Example:
Route :: get ('emp/{id}', function ($id) {
    echo 'Emp '.$id;
});

5. 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.
Example:
Route :: get ('emp/{desig?}', function ($desig = null) {
    echo $desig;
});
Route :: get ('emp/{name?}', function ($name = 'Guest') {
    echo $name;
});
Reactions

Post a Comment

0 Comments

close