Setup Menus in Admin Panel

  • No products in the cart.

  • LOGIN

Routing

Routing in Laravel


Routing is the act of linking a URL to a function in your application. In Laravel, it is possible to route in two ways. You can either route to a closure or a controller action. Here we are going cover controllers in more detail.  We can route to a closuRoutes in Laravel are declared in routes.php direcotry. This file will represent the connection between your site’s URLs and the functions that contain application logic for your site. This is very handy as other developers will be able to come into your project and know how requests are routed, simply by reviewing this file.

Here is a simple example of routing to a closure:

Route::get('Welcome', function()
{
return "Welcome to laravel course.";
});

We’re using the Route::get() method to define the route. Route::get() registers a closure with the router that specifically responds to a GET request at the specified URI. To register a closure for the POST, PUT, and DELETE requests, you will use:

Route::post(),
Route::put(),and
Route::delete()

These methods correspond to what are commonly referred to as the HTTP verbs. Developers only interact with the GET and POST requests. When a user clicks on a link or enters a URL in their address bar, they’re creating a GET request. When a user submits a form, they’re typically creating a POST request. The first argument for the Route::get() method is the URI for the route (the part of the URL
after the domain name), and the second argument is the closure which contains the desired application logic.

 

Named Routing:

Named routing is another amazing feature of Laravel framework. Named routes allow referring to routes when generating redirects or Url’s more comfortably.

You can specify named routes by chaining the name method onto the route definition:

Route::get('user/profile', function () {
//
})->name('profile');

You can specify route names for controller actions:

Route::get('user/profile', '[email protected]')->name('profile');

Once you have assigned a name to your routes, you may use the route’s name when generating URLs or redirects via the global route function:

// Generating URLs...
$url = route('profile');

// Generating Redirects...
return redirect()->route('profile');

 

 

Laravel reverse routing

Laravel reverse routing is generating URL’s based on route declarations. Reverse routing makes your application so much more flexible. It defines a relationship between links and Laravel routes. When a link is created by using names of existing routes, appropriate Uri’s are created automatically by Laravel. Here is an example of reverse routing.

// route declaration
Route::get(‘login’, ‘[email protected]’);

Using reverse routing we can create a link to it and pass in any parameters that we have defined. Optional parameters, if not supplied, are removed from the generated link.

{{ HTML::link_to_action(‘[email protected]’) }}

It will automatically generate an Url like http://abc.com/login in view.

 

ASSIGNMENT : What are named routes in Laravel? MARKS : 10  DURATION : 10 minutes

ASSIGNMENT : What is reverse routing in Laravel? MARKS : 10  DURATION : 10 minutes

SEE ALL Add a note
YOU
Add your Comment
 

Advanced Course Search Widget

Popular Courses

Course Categories

© PHPhelp. 2002 - 2018 All rights reserved.
X