How do we know where the route came from and redirect it back?

Asked

Viewed 1,056 times

0

I have 3 tables on BD: Emitentes, Pessoas e Cidades. Where on the table Issuers and in the table People have the FK id_cidade. When I’m registering one, both a Emitente how much a Pessoa I have in the views a link that redirects me to Cidades:

Issuers/Persons:

{!! Form::label('id_cidade', 'Cidade:', ['class' => 'control-label']) !!}
{!! Form::text('id_cidade', $id_cidade, ['class' => 'form-control',  'readonly']) !!}
{!! Form::label('uf', 'UF:', ['class' => 'control-label']) !!}
{!! Form::text('uf', $nomeestado, ['class' => 'form-control', 'readonly']) !!}
<a href="{{ route('cidades.index') }}" class="btn btn-info" onClick="save()"> Selecionar&nbsp;&nbsp;<span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span></a>

And on the page cidades.index I have a link that redirects me to the Issuers:

<tbody>@foreach($cidades as $cidade)<tr>
<td>
<a href="{{ URL::to('cidades/' . $cidade->id . '/selecionacidade') }}" class="btn btn-success">&nbsp;&nbsp;<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>&nbsp;&nbsp;</a>
</td>
<td>{{ e($cidade->cidade) }}</td>
<td>{{ e($cidade->estado) }}</td>

Route:

Route::get('cidades/{id}/selecionacidade','CidadesController@selecionacidade');

Controller Cidades:

public function selecionacidade($id, Request $request){
  $request->session()->put('selecionacidade', 1);  
  $cidade = Cidade::findOrFail($id);  
  $request->session()->put('codigocidade', $id);  

  $request->session()->put('nomecidade', $cidade->cidade);
  $request->session()->put('nomeestado', $cidade->estado);
  $caminhocidade = $request->session()->get('caminhocidade'); //url de edição ou inserção
  $emitenteid = $request->session()->get('emitenteid');  
      return redirect()->route($caminhocidade, $emitenteid); } 

Everything works perfectly to register Issuers, but I tried to do the same for the view of People, I did a new function on CidadesController to select the city to register People, I made a new route, and she kept redirecting me to the view of registration of Issuers. So I wondered if I could know what view I was directed, and if I could be directed back to that view with the value id_cidade stored. Thus working for both criminal records.

I apologise for the extensive question, but I saw no other way to specify what I needed.

  • 1

    Looking like this Route::get('cidades/{id}/selecionacidade','CidadesController@selecionacidade'); wouldn’t it be better to put an extra parameter to define where it will come back? type Route::get('cidades/{id}/selecionacidade/{volta}','CidadesController@selecionacidade'); ????????

  • 1

    Yes, you’re absolutely right. It was your comment I was waiting for. Thank you very much!

  • A hint: I see many sites using the return url in the url itself. You could use a parameter get to do this. Depending on the context, I would also not use session to save the data, but rather a record in the database.

1 answer

2


If the goal is to know where the route came from, you could capture globally or in a controller-specific method which is the current url and send it to session.

session(['url.previous' => $request->url()]);

return redirect('/novo_redirecionamento');

On the other page you would have the value of the previous url saved in session('url.previous').

It is also possible to use headers for this:

$request->headers->get('referer')

Browser other questions tagged

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