I have a welcome page which have some menu to navigate through specific areas in the main page using hashtags like #about-sec but if I go to different page like https://example.com/encyclopedia and rom there the menu items contain hashtags won't work because it will point to current page not the main page like https://example.com/encyclopedia#about-sec so I would like the check the url within blade to see if current url is https://example.com/encyclopedia than direct me to main page route with hashtag to be like https://example.com/#about-sec. I used the code below but it won't work so Is there another way to make it work properly ?
@Q8Xbox You have the line of code already: route('welcome')->withFragment('#about-sec') . You don't need to check whether your own the encyclopedia page or welcome page, the browser can do that for you. If you're on the encyclopedia page, it will navigate to the home page and then find the fragment. If you're already on the home page, it will just scroll to the fragment.
@rooskie it doesn't work (for Laravel 10 at least) because function route returns string.
See source in vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:
/**
* Generate the URL to a named route.
*
* @param string $name
* @param mixed $parameters
* @param bool $absolute
* @return string
*/
function route($name, $parameters = [], $absolute = true)
{
return app('url')->route($name, $parameters, $absolute);
}
And, because of that, we get an error "Call to a member function withFragment() on string"
@Snapey This works fine if I'm in welcome page and if navigate to different page like {{ route('encyclopedia') }} which navigate to http://localhost:8000/encyclopedia it will not work anymore when you click on them will do nothing.