Laravel 5.1-5.8 中以前的路由名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40690202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:46:40  来源:igfitidea点击:

Previous route name in Laravel 5.1-5.8

laravellaravel-routing

提问by Akul Von Itram

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

我尝试在 Laravel 5.1 中查找之前路线的名称。和:

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

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

我得到了路由 url,但我尝试像当前页面一样获取路由名称:

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

My client wont a different text for Thank you page, depends on from page (Register pageor 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

回答by Akul Von Itram

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

这里有什么对我有用。我找到了这个答案和这个问题,并将其修改为适用于我的情况: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 versionby Robert

更新版本5.8罗伯特·

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

回答by Alexey Mezenin

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());
    
  1. 检查以前的 URL 而不是路由名称。

  2. 使用会话。首先,保存路由名称:

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

Then check if session has previous-route:

然后检查会话是否有previous-route

@if (session()->has(`previous-route`) && session(`previous-route`) == 'contacts')
    Display something
@endif
  1. Use GETparameters to pass route name.
  1. 使用GET参数传递路由名称。

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

如果我是你,我会使用会话或检查以前的 URL。

回答by Ryuta Hamasaki

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;
}

回答by Dip

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