Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Previous route name in Laravel 5.1-5.8

I try to find name of previous route in Laravel 5.1. With:

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

I get the route url, but I try to get route name like I get for current page:

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

My client wont a different text for Thank you page, depends on from page (Register page or Contact page) user go to Thank you page. I try with:

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

But that didn't work. I try to get something like:

@if(previous-route == 'contact')
  some text
@else
  other text
@endif
like image 988
Akul Von Itram Avatar asked Nov 19 '16 07:11

Akul Von Itram


People also ask

What is named routing in Laravel?

Named routing is another amazing feature of Laravel framework. Named routes allow referring to routes when generating redirects or Urls more comfortably. You can specify named routes by chaining the name method onto the route definition: Route::get('user/profile', function () { // })->name('profile');

Where is the routes folder in Laravel?

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web.php file defines routes that are for your web interface.


4 Answers

Here what work for me. I find this answer and this question and modify it to work in my case: https://stackoverflow.com/a/36476224/2807381

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

Update for 5.8 version by Robert

app('router')->getRoutes()->match(app('request')->create(url()->previous()))->getName()
like image 100
Akul Von Itram Avatar answered Nov 16 '22 00:11

Akul Von Itram


Simply you can do this to achieve it. I hope it helps

In Controller:

$url = url()->previous();
$route = app('router')->getRoutes($url)->match(app('request')->create($url))->getName();

if($route == 'RouteName') {
    //Do required things
 }

In blade file

@php
 $url = url()->previous();
 $route = app('router')->getRoutes($url)->match(app('request')->create($url))->getName();
@endphp

@if($route == 'RouteName')
   //Do one task
@else
  // Do another task
@endif
like image 34
Biswa Avatar answered Nov 16 '22 01:11

Biswa


You can't get route name of previous page, so your options are:

  1. Check previous URL instead of a route name.

  2. Use sessions. First, save route name:

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

Then check if session has previous-route:

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

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

like image 25
Alexey Mezenin Avatar answered Nov 16 '22 00:11

Alexey Mezenin


I've 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;
}
like image 39
Ryuta Hamasaki Avatar answered Nov 16 '22 00:11

Ryuta Hamasaki