Laravel 5.1-5.8 中的先前路线名称
Previous route name in Laravel 5.1-5.8
我尝试在 Laravel 5.1 中查找上一条路线的名称。
有:
{!! URL::previous() !!}
我得到了路线 url,但我尝试像我为当前页面获得的那样获得路线名称:
{!! Route::current()->getName() !!}
我的客户不会为 谢谢页面 提供不同的文本,具体取决于来自页面(注册页面 或 联系方式页面) 用户转到感谢页面。我尝试:
{!! Route::previous()->getName() !!}
但这没有用。我试着得到类似的东西:
@if(previous-route == 'contact')
some text
@else
other text
@endif
您无法获取上一页的路线名称,因此您的选择是:
检查前面的 URL 而不是路线名称。
使用sessions。首先,保存路线名称:
session()->flash('previous-route', Route::current()->getName());
然后检查会话是否有previous-route
:
@if (session()->has(`previous-route`) && session(`previous-route`) == 'contacts')
Display something
@endif
- 使用
GET
参数传递路由名称。
如果我是你,我会使用会话或检查以前的 URL。
这是对我有用的。我找到了这个答案和这个问题并将其修改为适用于我的情况:
@if(app('router')->getRoutes()->match(app('request')->create(URL::previous()))->getName() == 'public.contact')
Some text
@endif
5.8 版本更新 Robert
app('router')->getRoutes()->match(app('request')->create(url()->previous()))->getName()
我创建了一个这样的辅助函数。
/**
* 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;
}
简单地说,你可以这样做来实现它。希望对你有帮助
在控制器中:
$url = url()->previous();
$route = app('router')->getRoutes($url)->match(app('request')->create($url))->getName();
if($route == 'RouteName') {
//Do required things
}
在blade文件中
@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
我尝试在 Laravel 5.1 中查找上一条路线的名称。 有:
{!! URL::previous() !!}
我得到了路线 url,但我尝试像我为当前页面获得的那样获得路线名称:
{!! Route::current()->getName() !!}
我的客户不会为 谢谢页面 提供不同的文本,具体取决于来自页面(注册页面 或 联系方式页面) 用户转到感谢页面。我尝试:
{!! Route::previous()->getName() !!}
但这没有用。我试着得到类似的东西:
@if(previous-route == 'contact')
some text
@else
other text
@endif
您无法获取上一页的路线名称,因此您的选择是:
检查前面的 URL 而不是路线名称。
使用sessions。首先,保存路线名称:
session()->flash('previous-route', Route::current()->getName());
然后检查会话是否有previous-route
:
@if (session()->has(`previous-route`) && session(`previous-route`) == 'contacts')
Display something
@endif
- 使用
GET
参数传递路由名称。
如果我是你,我会使用会话或检查以前的 URL。
这是对我有用的。我找到了这个答案和这个问题并将其修改为适用于我的情况:
@if(app('router')->getRoutes()->match(app('request')->create(URL::previous()))->getName() == 'public.contact')
Some text
@endif
5.8 版本更新 Robert
app('router')->getRoutes()->match(app('request')->create(url()->previous()))->getName()
我创建了一个这样的辅助函数。
/**
* 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;
}
简单地说,你可以这样做来实现它。希望对你有帮助
在控制器中:
$url = url()->previous();
$route = app('router')->getRoutes($url)->match(app('request')->create($url))->getName();
if($route == 'RouteName') {
//Do required things
}
在blade文件中
@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