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')
.
Makes a mistake ?
– Diego Souza
@Diegosouza no, just send null
– scooby