12

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?

1

5 Answers 5

15

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 } 
Sign up to request clarification or add additional context in comments.

Comments

6

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.

2 Comments

for this my url generated as localhost/travel/tr/approveRequest?id=SR4%3D i wrote route as Route::get('approveRequest/{par1}', 'Gslab\Travel\TravelRequestController@approveTravelRequest'); and on controller side public function approveTravelRequest($par1) { echo $id = $par1; exit; } still it is not taking route is not getting
Don't put parameter in the route definition. See how I did. Because you are using optional parameter with question mark ?.
5
Route::get('edit/{id}', 'CommentController@edit')->name('edit'); public function edit($id) { // use the variables } <a href="{{route('edit',$value->id)}}">edit</a> 

2 Comments

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
1

there are 3 ways i used:

1-way with controller

Route::get('post', 'PostController@store'); public function store(Request $request) { $name = $request->input('name'); // get your params } 

2-way with route closure

Route::get('/', function (Request $request) { $name = $request->name //you can use this without input('name') }); 

3- with params in controller

Route::get('comment/{var}/{var2}', 'CommentController@edit'); public function approve ($var, $var2) { // use the variables } 

Comments

1

There are many methods to do this.

Method 1

If you make the url like below:

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

You can make route with parameters as like below.

 Route::get('approveRequest/{par1}/{par2}/{par3}', 'ApproveController@approve'); 

And in your function get the respective parameter as following:

public function approve($par1,$par2,$par3,) { $id = $par1; $gsID = $par2; .... and so on for all your variables. } 

Method 2 use request method:

If your url is like:

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

Then define Route Like:

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

Then in your controller function you get parameters as below:

Use Request after the namespace in youe controller class

namespace App\Http\Controllers; use Illuminate\Http\Request; class SomeClassController extends Controller { public function approve(Request $request) { $id = $request->id; $gsID = $request->get('gsID'); .... and so on for all your variables. } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.