Pass parameter from one route to another

7

I need to get a url variable and redirect with the parameter to another function

Route::get('doacao/{id}', function(Request $request){
   return Redirect::route('escolha/doacao')->with('id',$request->id);
});

Redirecting to this other route to get the parameter in the @retornaAssociado function:

Route::get('escolha/doacao', ['as' => 'escolha/doacao', 'uses' => 'Site\CadastroController@retornaAssociado']);

I tried several ways to get the id inside the function, but I could not. I need two routes because I do not want the id to appear in the url when I return the view.

public function retornaAssociado(Request $request){
   $data = $request->all(); 

   return view('layouts/pagamento')->with('id', $data['id']);  

}
    
asked by anonymous 02.02.2017 / 13:21

1 answer

5

Would not the correct one pass a $id variable to capture the route parameter?

Route::get('doacao/{id}', function(Request $request, $id){
   return Redirect::route('escolha/doacao')->with('id',$id);
});

Then in the following route, to capture this parameter, you need to get the value that is in session .

public function retornaAssociado(Request $request){

   return view('layouts/pagamento')->with('id', session('id'));  

}

When you use the with method of Redirect , you are telling laravel to store the data in a session flash. I think this is not the case, because if you update pages, that data will disappear. This is because the flash, after it is used, is removed from the session

I think you'd better add an optional parameter on the second route, to get this parameter from another url, but if you do not receive it, the page is also displayed normally.

So:

Route::get('escolha/doacao/{id?}', ['as' => 'escolha/doacao', 'uses' => 'Site\CadastroController@retornaAssociado']);

public function retornaAssociado(Request $request, $id = null){

   return view('layouts/pagamento')->with('id', $id);  

}

If someone accesses escolha/doacao/1 , the value of $id will be 1 . If you access escolha/doacao , the value will be NULL .

But note also that it is necessary, in the act of redirection, to pass as parameter $id , so that it is redirected to that route with the same parameter:

Route::get('doacao/{id}', function(Request $request, $id){
    return Redirect::route('escolha/doacao', $id);
});

The above code will result in a redirect for "escolha/doacao/{$id}" . So, if someone accesses doacao/5 , it will be redirected to escolha/doacao/5 . But it does not prevent the person from accessing escolha/doacao directly.

Update

If the question's intention is to redirect to another url, by hiding the value of $id passed in doacao/$id , I suggest using session (I'm not talking about with , since the value is temporary).

You could do:

Route::get('doacao/{id}', function(Request $request, $id){

   session('doacao.id', $id);

   return Redirect::route('escolha/doacao');
});

To recover this value, just call session('doacao.id') .

    
02.02.2017 / 13:34