php Laravel 返回按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12333739/
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-08-25 03:22:08  来源:igfitidea点击:

Laravel back button

phplaravel

提问by cosapostolo

I am trying to create a simple back button on a page. The user can arrive to this page from two different pages so I would like to know from which page he arrived. Is that possible?

我正在尝试在页面上创建一个简单的后退按钮。用户可以从两个不同的页面到达这个页面,所以我想知道他是从哪个页面到达的。那可能吗?

回答by Anon

In Laravel, you can do something like this: <a href="{{Request::referrer()}}">Back</a>(assuming you're using blade).

在 Laravel 中,您可以执行以下操作:(<a href="{{Request::referrer()}}">Back</a>假设您使用的是刀片)。

In Laravel 4

在 Laravel 4

{{ URL::previous() }}

Laravel documentation

Laravel 文档

回答by Josh Griggs

I know this is an oldish question but I found it whilst looking for the same solution. The solution above doesn't appear to work in Laravel 4, you can however use this now:

我知道这是一个古老的问题,但我在寻找相同的解决方案时发现了它。上面的解决方案似乎在 Laravel 4 中不起作用,但是您现在可以使用它:

<a href="{{ URL::previous() }}">Go Back</a>

Hope this helps people who look for this feature in L4

希望这对在 L4 中寻找此功能的人有所帮助

(Source: https://github.com/laravel/framework/pull/501/commits)

(来源:https: //github.com/laravel/framework/pull/501/commits

回答by Akshay Khale

Laravel 5.2+, back button

Laravel 5.2+,后退按钮

<a href="{{ url()->previous() }}" class="btn btn-default">Back</a>

回答by mike.bronner

The following is a complete Blade (the templating engine Laravel uses) solution:

下面是一个完整的 Blade(Laravel 使用的模板引擎)解决方案:

{!! link_to(URL::previous(), 'Cancel', ['class' => 'btn btn-default']) !!}

The options array with the class is optional, in this case it specifies the styling for a Bootstrap 3 button.

带有类的选项数组是可选的,在这种情况下,它指定 Bootstrap 3 按钮的样式。

回答by Thiago Cardoso

Indeed using {{ URL:previous() }}do work, but if you're using a same named route to display multiple views, it will take you back to the first endpoint of this route.

确实使用{{ URL:previous() }}确实有效,但是如果您使用相同命名的路由来显示多个视图,它将带您回到该路由的第一个端点。

In my case, I have a named route, which based on a parameter selected by the user, can render 3 different views. Of course, I have a default case for the first enter in this route, when the user doesn't selected any option yet.

就我而言,我有一个命名路由,它基于用户选择的参数,可以呈现 3 个不同的视图。当然,当用户还没有选择任何选项时,我对这条路线中的第一次进入有一个默认情况。

When I use URL:previous(), Laravel take me back to the default view, even if the user has selected some other option. Only using javascript inside the button I accomplished to be returned to the correct view:

当我使用 时URL:previous(),即使用户选择了其他选项,Laravel 也会带我回到默认视图。仅在我完成的按钮内使用 javascript 才能返回到正确的视图:

<a href="javascript:history.back()" class="btn btn-default">Voltar</a>

I'm tested this on Laravel 5.3, just for clarification.

我在 Laravel 5.3 上对此进行了测试,只是为了澄清。

回答by mhankins

On 5.1 I could only get this to work.

在 5.1 我只能让它工作。

<a href="{{ URL::previous() }}" class="btn btn-default">Back</a>

回答by user9747893

One of the below solve your problem

以下之一解决您的问题

URL::previous() 
URL::back()

other

其他

URL::current()

回答by Sohanur Rahman

You can use javascript for this provblem. It's retrieve link from browser history.

您可以使用 javascript 来解决这个问题。它是从浏览器历史记录中检索链接。

<script>
function goBack() {
  window.history.back();
}
</script>
<button onclick="goBack()">Go Back</button>

回答by Long Lê Hoàng

You can use {{ URL::previous() }} But it not perfect UX.

您可以使用 {{ URL::previous() }} 但它不是完美的用户体验。

For example, when you press F5 button and click again to Back Button with {{ URL::previous() }} you will stay in.

例如,当您按 F5 按钮并再次单击带有 {{ URL::previous() }} 的后退按钮时,您将停留在。

A good way is using {{ route('page.edit', $item->id) }} it always true page you wanna to redirect.

一个好方法是使用 {{ route('page.edit', $item->id) }} 它总是你想要重定向的真实页面。

回答by davidkihara

<a href="{{ url()->previous() }}" class="btn btn-warning"><i class="fa fa-angle-left"></i> Continue Shopping</a>

This worked in Laravel 5.8

这在 Laravel 5.8 中有效