以前的路线名称在Laravel 5.1-5.6中

Aku*_*ram 8 laravel laravel-routing

我试着在Laravel 5.1中找到以前路线的名字.附:

{!! URL::previous() !!}
Run Code Online (Sandbox Code Playgroud)

我得到路由网址,但我尝试获取当前页面的路由名称:

{!! Route::current()->getName() !!}
Run Code Online (Sandbox Code Playgroud)

我的客户不会为谢谢页面的不同文本,取决于从页面(注册页面联系页面)用户转到谢谢页面.我试着用:

{!! Route::previous()->getName() !!}
Run Code Online (Sandbox Code Playgroud)

但那没用.我尝试得到类似的东西:

@if(previous-route == 'contact')
  some text
@else
  other text
@endif
Run Code Online (Sandbox Code Playgroud)

Aku*_*ram 23

这对我来说有什么用.我找到了这个答案和这个问题并修改它以适用于我的情况:https://stackoverflow.com/a/36476224/2807381

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

  • app('router')-> getRoutes()-> match(app('request')-> create(url()-> previous()))-> getName()这是5.8中的工作示例 (3认同)

小智 8

只需这样做即可实现它。我希望它有帮助

在控制器中:

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

if($route == 'RouteName') {
    //Do required things
 }
Run Code Online (Sandbox Code Playgroud)

在刀片文件中

@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
Run Code Online (Sandbox Code Playgroud)


Ale*_*nin 6

您无法获取上一页的路线名称,因此您可以选择:

  1. 检查以前的URL,而不是路由名称。

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

    session()->flash('previous-route', Route::current()->getName());
    
    Run Code Online (Sandbox Code Playgroud)

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

@if (session()->has(`previous-route`) && session(`previous-route`) == 'contacts')
    Display something
@endif
Run Code Online (Sandbox Code Playgroud)
  1. 使用GET参数传递路由名称。

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