0

I want to redirect to route with name doug-test, but I want to preserve the url parameter. I saw a webpage that says use $request->query('url') to get the url parameter, but that doesn't seem to work. I want to know the value of the get parameter "url".

For example, if someone goes /login?url=/xyz

I want them redirected to /dougs-page?url=/xyz where /dougs-page is a route named "doug-test"

Here's what I have so far:

Route::get('/login', function (Request $request) {return redirect()->route('doug-test', ['url'=> $request->query('url')]);})->middleware('not-auth')->name('login');

The error I'm getting is "Call to undefined method Illuminate\Support\Facades\Request::query()"

dougd_in_nc
  • 371
  • 5
  • 20
  • 1
    you don't typehint the facade `Request` as you don't want an instance of a facade as it is a static proxy for another object ... if you want an instance of something you want the actual `Request` class (`Illuminate\Http\Request`) which the facade is a proxy for. ... https://laravel.com/docs/8.x/facades#facade-class-reference – lagbox Nov 30 '22 at 05:03
  • @lagbox I don't totally understand how you are saying the code should look. Are you saying take out the class name "Request" ? – dougd_in_nc Nov 30 '22 at 05:33
  • you are aliasing the wrong `Request` class – lagbox Nov 30 '22 at 13:27

2 Answers2

0

Nevermind. Taking a closer look at the docs for Request, I see query() is a static method. This works:

Route::get('/login', function (Request $request) {return redirect()->route('doug-test', ['url'=> $request::query('url')]);})->middleware('not-auth')->name('login');
dougd_in_nc
  • 371
  • 5
  • 20
-1

I think this one answers your question. How to get the http referer in laravel?

url()->previous();

msmahon
  • 453
  • 2
  • 11
  • Thanks, but not really. I'm asking a different question. Just generally, I want to get one parameter from the current request – dougd_in_nc Nov 29 '22 at 17:01
  • Do you want the original URL the user is requesting before they are redirected? When you say parameter, I think of URL query parameters that would be passed from a form, like `example.com/?name=joe`, but I don't think that is what you mean. – msmahon Nov 29 '22 at 17:06
  • 1
    url just happens to be the name of a parameter here, it could be any parameter name with any value. I just want to redirect and save a parameter value. I just added a clarification to question. And, yes, I mean URL parameters. Although, I figured out what was wrong. – dougd_in_nc Nov 29 '22 at 17:26