Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel route with parameters

Tags:

laravel

routes

Am using Laravel route for approving some form from email. So i have encrypted some variables and created link which is like:

<a href="http://localhost/travel/tr/approveRequest?id=<?=$Encoded_travelRq_id?>&gsID<?=$Encoded_emp_gslab_id?>&decrypt<?=$Encoded_iv?>/">Approve</a>;

Now how route can be written for this on Laravel side in which i can seperate the variables like id, gsID, decrypt from url and can send to controller's function used against that route?

like image 441
Prit Avatar asked Feb 21 '17 05:02

Prit


People also ask

What is route parameters in Laravel?

The required parameters are the parameters that we pass in the URL. Sometimes you want to capture some segments of the URI then this can be done by passing the parameters to the URL. For example, you want to capture the user id from the URL. Let's see the example without route parameters.

How do you get parameters in Laravel?

To retrieve the query parameters on your Laravel backend, you can make use of either the "Request" class or the "request()" helper method. Imagine you want to get the "search" query from the URL, you can do as follows. $searchQuery = $request->query('search');

What are routes in Laravel explain route handling with controller and route parameters?

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.


2 Answers

Usually I use two ways:

1º way:

The route:

Route::get('approveRequest', 'ApproveController@approve');

The controller:

public function approve (Request $request) {
    $var1 = $request->input('var1');
    $var2 = $request->input('var2');
    
    // (...) do something with $var1 and $var2
}

2º way:

The route:

Route::get('approveRequest/{var1}/{var2}', 'ApproveController@approve');

The controller:

public function approve ($var1, $var2) {
    
    // (...) do something with $var1 and $var2: they already have a instance
}
like image 118
Caio César Brito Avatar answered Nov 10 '22 10:11

Caio César Brito


Simply write a GET route to approveRequest :

Route::get('approveRequest', 'ApproveController@approve');

Because you are using URL parameters, you can simply get them in the approve() function like this

public function approve(Request $request) 
{
     $id = $request->id;
     $gsID = $request->get('gsID');
     .... and so on for all your variables.
}

With this approach the order of parameters does not matter.

like image 23
EddyTheDove Avatar answered Nov 10 '22 09:11

EddyTheDove