Previous route name in Laravel 5.1-5.8

I am trying to find the name of a previous route in Laravel 5.1. WITH:

{!! URL::previous() !!} 

I get the route URL, but I'm trying to get the route name, as I get for the current page:

 {!! Route::current()->getName() !!} 

My client will not use other text for the "Thank you" page, depending on the page (registration page or "Contact" page), the user goes to the "Thank you" page. I am doing my best:

 {!! Route::previous()->getName() !!} 

But that did not work. I am trying to get something like:

 @if(previous-route == 'contact') some text @else other text @endif 
+7
source share
3 answers

This is what works for me. I find this answer and this question and modify it to work in my case: fooobar.com/questions/547973 / ...

 @if(app('router')->getRoutes()->match(app('request')->create(URL::previous()))->getName() == 'public.contact') Some text @endif 

Update for version 5.8 from Robert

 app('router')->getRoutes()->match(app('request')->create(url()->previous()))->getName() 
+17
source

You cannot get the route name of the previous page, so you can choose the following options:

  1. Check the previous URL for the route name.

  2. Use sessions . First save the name of the route:

     session()->flash('previous-route', Route::current()->getName()); 

Then check if the session has a previous-route :

 @if (session()->has('previous-route') && session('previous-route') == 'contacts') Display something @endif 
  1. Use GET parameters to pass the name of the route.

If I were you, I would use sessions or check the previous URL.

+5
source

I created a helper function like this.

 /** * Return whether previous route name is equal to a given route name. * * @param string $routeName * @return boolean */ function is_previous_route(string $routeName) : bool { $previousRequest = app('request')->create(URL::previous()); try { $previousRouteName = app('router')->getRoutes()->match($previousRequest)->getName(); } catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception) { // Exception is thrown if no mathing route found. // This will happen for example when comming from outside of this app. return false; } return $previousRouteName === $routeName; } 
0
source

Source: https://habr.com/ru/post/1260000/


All Articles