Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

benjivm's avatar
Level 11

Using forceRootUrl() breaks some routing

I have my Laravel app running from a subdirectory, so in order to be able to use the URL helper like this: {{ url('users') }} instead of like this {{ url('subdirectory/users') }} I use the forceRootUrl() method in my AppServiceProvider.php file. It looks like this:

public function boot()
    {
        URL::forceRootUrl(Config::get('app.url'));

...

And my .env: APP_URL=http://app.dev/subdirectory

So far so good, except when returning routes. For example I have a sorting method that does this:

function sort_schedule($column, $order)
{
    return route('schedule', [
                 'sortBy' => $column,
                 'sortOrder' => $order,
            ]
         );
}

But the link it generates is this: http://app.dev/subdirectory/subdirectory/...

The same thing happens when I use Kyslik's Column Sortable package.

Edit:

I did some investigating and the problem seems to originate in the Request::path() method. So doing something like this:

return("<a href='". request()->path() ."/test'>Test</a>");

Results in the same thing:

http://app.dev/subdirectory/subdirectory/test

The actual HTML markup is this, however:

<a href='subdirectory/test'>Test</a>

So how can I make the request path drop off the first part?

0 likes
0 replies

Please or to participate in this conversation.