rotaercz's avatar

Is there a way to get the previous url path?

I've tried:

url()->previous()

and this gives me the full url like the following:

http://testdev.com/etc

Is there a way to just get the following without having to get the substring from the above?

/etc
0 likes
11 replies
rotaercz's avatar

I think you may have misunderstood my question. I'm asking if there is a helper method in Laravel to get the path without having to get the substring from the full url.

url()->previous()

returns the correct full url that I need with the domain name. I'm asking if I can get the path without the domain name without having to manipulate the string.

5 likes
jlrdw's avatar

There is a session helper to store such things. Did you look at the helpers in the docs

https://laravel.com/docs/5.4/helpers

I'm asking if I can get the path without the domain name without having to manipulate the string.

What are you trying to do, you said return to previous page?

rotaercz's avatar

I have looked at the docs. There's not enough information there. And that's why I'm asking here to see if anyone happens to know if there is a method.

I'm not trying to return to the previous page. I'm building a database and creating my own analytics based on how people are traversing my site and storing the paths that people are taking.

Stratos's avatar

He needs a helper to retrieve only the part after domain name of previous url.

1 like
jlrdw's avatar

Then $uri = $request->path();

But that isn't what was asked, previous page was asked, sorry.

Nash's avatar
Nash
Best Answer
Level 20

I'm not trying to return to the previous page. I'm building a database and creating my own analytics based on how people are traversing my site and storing the paths that people are taking.

Why not do this by storing the current path when a page is visited (instead of the previous one)? As long as you have some kind of identifier (like a session or user ID), you can still figure out how people are traversing. As @jlrdw pointed out, there is a helper for retrieving the current path.

As for the previous path, there doesn't seem to be a helper, but you can easily make your own. Just str_replace the base url with an empty string to get the path, e.g.

str_replace(url('/'), '', url()->previous())

Edit: updated the example

10 likes
josedh's avatar

I done this a few time with laravel best way I've done it is:

public function store(Request $request) { $temp = $request->session()->get('_previous'); echo $temp['url']; ... ...

Laravel stores the previous URL in a session called "_previous" which is itself an array.

TobiasS's avatar

Old post, but since I tried to find the info myself, here is a solution

$request->session()->previousUrl()
2 likes
qodli_zaka_513's avatar

I had another option using the PHP function parse_url():

parse_url(url()->previous(), PHP_URL_PATH);

Please or to participate in this conversation.