Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove part of the segment from url laravel

Tags:

url

php

laravel

I have a url like this:

http://example.com/en/search

Now I want to check the first segment of the url like

$lang = Request::segment(1);

And I want to remove the $lang segment if it matches to the certain conditions. I'm retieving the url like this

$url = Request::fullUrl();

Now I want to reconstruct the url to something like this

http://example.com/search

Please note I cannot use str_replace because it searches in all parts of the url, I just want to search and replace in the first segment.

like image 934
sam Avatar asked Jan 18 '26 02:01

sam


1 Answers

If you just want to make a quick check, then remove the first segment and redirect the user, you could easily do e.g. like this (for Laravel 5):

$segments = Request::segments();
$first = array_shift($segments);
if (some_condition) {
    return redirect()->to(implode('/', $segments));
}
like image 77
Joel Hinz Avatar answered Jan 19 '26 16:01

Joel Hinz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!