Skip to content
Start » Web development » Return link to previous URL in Laravel

Return link to previous URL in Laravel

In some moments of your application or site developed with the Laravel framework, there is a need to, via a “link”, allow the user to go back or return to the URL – page or route, previously called.

This is commonly needed when showing a 404 – Page not found, or 403 – Forbidden error message screen.

However, it is not always interesting to mount this link through the history browser navigation, which can be achieved by the method back in Javascript.

The Javascript code example to return to the previous navigation URL would be:

history.back()

As I said above, it is not always interesting to use this function because our user may have reached this point in the system through redirects, and it is necessary to guarantee the previous route correctly.

Another problem is related to the SEO of your application, which can be harmed if the search engine does not correctly identify the links on the pages it crawls.

Mount link to previous URL or Route with Laravel

So the best strategy is to build this URL address using the components that Laravel provides the developer.

You can build the URL using two different strategies: in the Controller or directly in the View directly in the Template Blade. If you mount it in the controller you must pass this information to the view as a variable.

Redirect to previous URL in Controller

To direct the user directly from the controller to the previous route, you must return from an action of this controller that will send the correct redirect information to the browser.

This is a strategy that does not assemble the URL on the screen for the user, but it works if you are in a flow where your application needs to make the decision to redirect the user to the previous route.

Use it for this or the helper back():

function actionNoController() { ... // returns with redirect to the last route return back(); }

Or still using the helper redirect with the method back:

function actionNoController() { ... // returns with redirect to the last route return redirect()->back(); }

Using this strategy it is still possible to add variables of input of the user for the route being called, or error information:

function actionNoController() { ... // returns with redirect to the last route, with User Inputs and Error Message return redirect() ->back() ->withInput() ->withErrors(['My error message ']); }

But if you need to show the URL to the user, so that he clicks on it and a search engine can index this information, do it directly in the View.

Mount Previous URL in View with Blade

If you want to set up a link on the screen for the user to click and return to the page previously browsed by him, use the helper url which correctly assembles your app's URLs by calling your method previous:

<a  href="/en/{{ url()->previous() }}/">
    <i class="fa fa-arrow-circle-o-left"></i>
    <span>To go back</span>
</a>

So the user can click on the link and the search engine can correctly assemble the link tree of your site or application if necessary.


More about Laravel

Best Practices for Writing Clean Code
Neste artigo apresento dicas para ao desenvolver um software, escrever com boas práticas de código limpo, o que, em geral, significa escrever blocos de código mais legíveis, em alguns contextos, mais fluídos e, bem estruturados. Existe um engano por parte de pessoas desenvolvedoras em nível júnior, de que o "conceito" …
Laravel PHP – Class 'NumberFormatter' Not Found
When installing an application on a Windows server (project need, adaptation to the client's infrastructure), I came across the error "NumberFormatter Not Found". The first reaction was to check if I had missed any commits from my local PHP Laravel development environment to the Github repository, which had a fix…
How to resolve “COMPOSER_AUTH” does not match the expected JSON schema in Laradock
When using Laradock, accessing the Workspace container I have sometimes received the second error when trying to run php composer: "COMPOSER_AUTH" does not match the expected JSON schema, this may result in errors and should be resolved: – Boolean value found, but an object is required According to this issue of …
Free e-books on programming and computing in general
Looking for free e-books on programming, software development, and other areas of computing? This list includes these topics as well as books on software engineering and other subjects. The pandemic period has exploded the number of educational videos and online courses, both in production and in…
How to repopulate with old the input file upload field, in PHP and Laravel
After validating data entry forms, which return to the user with information about errors that must be corrected, the good practice of populating fields with the old values (old) cannot be applied to upload fields – field files , both in PHP Laravel…
Reasons to choose and use PHP as a programming language
Many beginners and even experienced developers, but who don't know the language, question what are the reasons to choose, and definitely use PHP as the programming language for a small or large project, first language or freelance project. Many love to hate the PHP language, yet…

keep up to date

Don't forget to follow me on twitter and sign the Mazer.dev channel on Youtube to receive updates on new articles, video lessons and free courses in software development and engineering.

Leave a Reply

Your email address will not be published.

en_USEnglish
%d bloggers like this: