Laravel 后面的按钮

我试图在页面上创建一个简单的返回按钮。用户可以到达这个网页从两个不同的网页,所以我想知道他从哪个网页到达。这可能吗?

153845 次浏览

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

Laravel 4

\{\{ URL::previous() }}

Laravel 5+

\{\{ url()->previous() }}

Laravel documentation

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:

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

Hope this helps people who look for this feature in L4

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

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

{!! 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.

Laravel 5.2+, back button

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

On 5.1 I could only get this to work.

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

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.

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.

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:

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

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

One of the below solve your problem

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

other

URL::current()

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

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

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

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

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

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

This worked in Laravel 5.8