Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - How to redirect with hash (#)

Tags:

laravel

I have link :

example.com/register#register

If validation fails laravel redirects to :

example.com/register

with validation errors bit without hash url part. How I can redirect to full url with # ?

I know I can use:

Redirect::to(route('register') . '#credits')

But I want complete solution so and my :

return back();

will redirect with #.

Maybe I need to override some code ?

like image 437
fico7489 Avatar asked Jul 13 '16 14:07

fico7489


2 Answers

You could create the URL first, using the route name.

$url = URL::route('route_name', ['#hash_tag']);

Redirect::to($url);

Or...

return Redirect::to(URL::previous() . "#hash_tag");
like image 150
Ben Rolfe Avatar answered Sep 23 '22 16:09

Ben Rolfe


But if you want to get the proper URL where hash is the fragment part and not a parameter you should use:

redirect(route('route_name', ['some_param_for_route']). '#hash')

instead of:

redirect()->route('route_name', [ 'some_param_for_route', '#hash' ])

so to get:

http://example.com/some_param_for_route#hash

and not:

http://example.com/some_param_for_route?#hash

this way you can also chain it further like for instance:

redirect(route('route_name', ['some_param']). '#hash')->with('status', 'Profile updated!');
like image 42
Picard Avatar answered Sep 23 '22 16:09

Picard