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')
.