Routing is one of the main concepts in laravel. When we make any request to laravel application they are mapped with the help route. It allows you to register routes that respond to any HTTP request. In this section, you will learn about the routing concept of Laravel.

All laravel application routes are registered in the web.php file. It is located in ‘routes/web.php’. This file tells Laravel to route all your application requests to its appropriate controller.

Creating routes

By default the file web.php looks like the following

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

Creating our own routes in laravel is explained as follows.

Route:: get ('/', function () {
    return view('home');
});
Route:: post('user/InsertUser', function () {
   return 'User added succcessfully';
});

Route::resource('User','UserController');

How Does Routing Works

  • First you execute your route. executing means that you write the URL on the browser and hit enter
  • The URL you entered should match with the one that you registered in web.php file. Not only the letter even their method type should match. If the route you registered is POST you should execute with POST. If it is GET, then you should execute using GET method. In our case lets take the first route which is ‘/’ (points to the root).
  • As a result of executing it will open home.blade.php which is located in ‘resource/views/home.blade.php’.
  • Because of we referred to the root ‘/‘ the router searches for the view which have a name that is specified inside the function parameter in our case which is return view(‘home’) So it will open home from the root file of resource/views/‘.

What if we want to define a route for the file which is found inside ‘resource/views/users/all.blade.php

Route:: get('user/all', function () {
   return view('user.all');
});

Executing ‘localhost:8000/user/all‘ opens all.blade.php.

Passing Parameters With Routes

If you need to capture the parameters passed with the URL. Here we have two types of routes .

  1. Required Parameter
  2. Optional parameter

You can define your routes as follows.

Required Parameters

Required parameters are parameters which should be mandatory for routing the web application. For example, you may need to capture a user’s ID from the URL.

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});

Optional Parameters

Sometimes you may need to specify a parameter, but you also want to make the presence of that route parameter optional. You may do so by placing a question mark(?) after the parameter name.

Route::get('user/{email?}', function ($email = null) {
    return $email;
});

Named Routes

It allow the convenient creating URL routes or redirects for specific routes. You may specify a name for a route by chaining the name method onto the route definition. you can define named routes as follows:

Route::get('user/history', 'UserController@history')->name('history'); 

The User controller will call for the function history.

Read about middlewares. It is tightly related with routes.

Resource: Laravel official documentation

Please feel free to tell us your feeling.

Categories: LaravelPHPWeb

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *