I have installed the Laravel in sub-folder and is trying to install the horizon. After routing to "test.com/sub-folder/horizon", all the design in broken and also the internal links are pointing to main domain instead of main-domain-without-subfolder.
After the search, it seems to be the known issue which is already reported in github issue
Has there is any work around to make horizon work when Laravel is installed in sub-folder?
I have a solution that only involves PHP.
The issue, as pointed out by @Isaiahiroko, is the
basePath
defined for Horizon's interface. That code is inLaravel\Horizon\Http\Controllers\HomeController::index()
. The idea is this: we are going to pass to Laravel's service container our own implementation of that controller that will override thebasePath
definition passed to Horizon's interface.Create a new controller with code like this:
What's left is telling Laravel's service container that when Horizon's
HomeController
is requested, it should provide ourHorizonHomeController
class. In yourAppServiceProvider
, at the end of theregister()
method, set this up:After that, you should be able to browse to
http(s)://<your-host>/<your-sub-dir>/horizon
normally.Considerations:
To me this feels cleaner that patching a compiled js, which also has the downside that needs to be re-applied every time Horizon is updated (this can be mitigated with a post-update script in
composer
, tho). Also, for additional points, this solution is only overriding the method that renders the view, but not the route, which means all of Horizon's authentication mechanisms (middlewares and gates) are working exactly as described in the documentation.