You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class AdminSpaController extends Controller
{
public function __invoke(?string $path = null): BinaryFileResponse|RedirectResponse
{
$index = public_path('admin/index.html');
abort_unless(is_file($index), 404);
$path = $path === null ? '' : trim($path, '/');
if ($path !== '' && $this->isStaticAsset($path)) {
abort(404);
}
if ($path !== '' && $path !== 'index.html') {
$hashUrl = '/admin/#/'.$path;
$query = request()->getQueryString();
if (is_string($query) && $query !== '') {
$hashUrl .= '?'.$query;
}
return redirect($hashUrl);
}
return response()->file($index, ['Content-Type' => 'text/html; charset=UTF-8']);
}
protected function isStaticAsset(string $path): bool
{
if (str_starts_with($path, 'assets/')) {
return true;
}
return (bool) preg_match('/\.(?:js|css|png|jpe?g|gif|svg|ico|woff2?|ttf|map|webp)$/i', $path);
}
}