Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oliuz/d112c2a66f28d10c58610f7f96b2b2b2 to your computer and use it in GitHub Desktop.
Save oliuz/d112c2a66f28d10c58610f7f96b2b2b2 to your computer and use it in GitHub Desktop.
Installing Laravel in a subfolder

Installing Laravel in a subfolder

Installing Laravel in a domain subfolder can be useful in certain situations, although it is not officially supported by Laravel creators. In this instruction, I will describe how to configure Laravel 10 in a subfolder and additional tools such as Livewire v3.

Table of Contents

  1. Laravel Basse
  2. Livewire
  3. Filament Panel Builder

Laravel Base

Create a .htaccess file in the main installation folder. In this file, rewrite all requests to the public folder:

RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]

Update the .env file by adding your folder name to the links. Additionally, create APP_DIR with the folder name:

APP_URL=http://localhost/subdir
ASSET_URL=http://localhost/subdir
APP_DIR=subdir

Add your subdirectory env variable to config/app.php:

return [
    //...
    'dir' => env('APP_DIR'),
    //...
];

It's a good idea to create a helper that will add a prefix to the paths. I did it in app/Helpers/RouteHelper.php:

use Illuminate\Support\Str;

class RouteHelper
{
    public static function getDirPathPrefix(?string $path = ''): string
    {
        $appDir = config('app.dir');

        if (! $appDir) {
            return $path;
        }

        if (Str::length($path) === 0) {
            return $appDir;
        }

        if (Str::startsWith($path, '/')) {
            $appDir = Str::start($appDir, '/');
        }

        return $appDir.Str::start($path, '/');
    }
}

In the app/providers/RouteServiceProvider.php file, modify the paths by adding prefixes:

$this->routes(function () {
    Route::middleware('api')
        ->prefix(RouteHelper::getDirPathPrefix('api'))
        ->group(base_path('routes/api.php'));

    Route::middleware('web')
        ->prefix(RouteHelper::getDirPathPrefix())
        ->group(base_path('routes/web.php'));
});

If you use the HOME constant from RouteServiceProvider in the code, change it to a static variable, and then update all occurrences of RouteServiceProvider::HOME to RouteServiceProvider::$HOME:

public static string $HOME = '/dashboard';

public function __construct($app)
{
    self::$HOME = RouteHelper::getDirPathPrefix('dashboard');
    parent::__construct($app);
}

Livewire

Create a new file named web_custom.php in the routes folder:

use Illuminate\Support\Facades\Route;
use Livewire\Features\SupportFileUploads\FilePreviewController;
use Livewire\Features\SupportFileUploads\FileUploadController;
use Livewire\Livewire;

Livewire::setScriptRoute(fn ($handle) => Route::get('/livewire/livewire.js', $handle));

Livewire::setUpdateRoute(fn ($handle) => Route::post('/livewire/update', $handle));

Route::post('/livewire/upload-file', [FileUploadController::class, 'handle'])
    ->name('livewire.upload-file');

Route::get('/livewire/preview-file/{filename}', [FilePreviewController::class, 'handle'])
    ->name('livewire.preview-file');

In the routes/web.php file, include the web_custom.php file at the end of the file:

require __DIR__.'/web_custom.php';

In this section, the routes are re-declared, which allows Laravel to automatically prepend the subdirectory prefix set earlier in the RouteServiceProvider. This way, there's no need to use RouteHelper::getDirPathPrefix in this section.

Filament Panel Builder

After configuration according to the documentation, the admin panel will return a 404. In the app/Providers/AdminPanelProvider.php file, you need to change the path:

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('admin')
            ->path(RouteHelper::getDirPathPrefix('admin'))
            //...
    }
}

⚠️ Filament uses Livewire, so to be able to log in or not fall into an infinite loop of redirects, you need to change the paths as described in the Livewire section above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment