Pass parameter from one route to another

Asked

Viewed 8,639 times

7

I need to take a variable from the url 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 @returned functionAssociated:

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

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

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

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

}
  • Makes a mistake ?

  • @Diegosouza no, just send null

1 answer

5


Would it be correct to pass a variable $id to capture the route parameter?

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

Then on the route to follow, to capture this parameter, you need to take the value that is in session.

public function retornaAssociado(Request $request){

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

}

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

I think it would be better if you add an optional parameter in the second route, to receive this parameter from another url, but if it does not receive, the page is displayed also normally.

Thus:

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 anyone accesses escolha/doacao/1, the value of $id will be 1. Access escolha/doacao, the value will be NULL.

But also note that it is necessary, in the act of redirecting, you pass as parameter the $id, to be 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 to "escolha/doacao/{$id}". So if anyone accesses doacao/5, will be redirected to escolha/doacao/5. But it doesn’t stop the person from accessing escolha/doacao directly.

Updating

If the author’s intention is to redirect to another url by hiding the value of $id past in doacao/$id, I suggest using session (I’m not talking about the with, because 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').

  • I tried this way too, but it does not recognize the parameter: Missing argument 1 for App Http Controllers Site Cadastrocontroller::returnAssociated()

  • 2

    @Scooby does not hide, in fact you are saying that it is optional. It may or may not have the value, you understand?

  • Ah I got it, thanks.

  • Wallace, in this case there is some way to send the parameter without being in the url? because this way when there is no parameter it returns null

  • the function seems to ignore ->with('id',$id) and only sees the url parameter

  • @Scooby instead of using with, you pass by parameter of the Redirect::route.

  • @Scooby now that I understand. You want to pass the parameter "invisible" to the user?

  • Exactly! I cannot show the id in the url

  • this way it also becomes visible: Return Redirect::route('choice/donation',array( 'id' => $request->id ));

  • @Scooby now do not understand: Why can not show the ID in the url, and the url already comes from a visible id?

  • Anyway, I’ve already answered your curiosity. Use session.

  • Does a Slug, uses token, uses hash, md5, bcrypt, sha1 or can use Session.

  • Pq I send this url directly from a function in the controller with a redirect, but thanks for the help I’ll look up the Session

  • @Scooby looks, all right. You know it. But I think if the ID is visible to the user, Session is unnecessary. p

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.