Skip to content

Instantly share code, notes, and snippets.

@benjibee
Last active January 10, 2023 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjibee/4e315a7db9e870f244b6807a83b90d40 to your computer and use it in GitHub Desktop.
Save benjibee/4e315a7db9e870f244b6807a83b90d40 to your computer and use it in GitHub Desktop.
Use ngrok URLs in Laravel for all assets, generated URLs, etc.
<?php
namespace App\Providers;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(\Illuminate\Http\Request $request)
{
/**
* Use ngrok URLs for assets and links
*
* If an ngrok URL is set in the APP_URL environment variable then force HTTPS and
* replace the HTTP_HOST header with the ngrok URL from HTTP_X_ORIGINAL_HOST
*/
if (
$request->server->has('HTTP_X_ORIGINAL_HOST')
&& Str::contains($request->server->get('HTTP_X_ORIGINAL_HOST'), 'ngrok.io')
) {
URL::forceScheme('https');
$request->server->set('HTTP_HOST', $request->server->get('HTTP_X_ORIGINAL_HOST'));
$request->headers->set('HOST', $request->server->get('HTTP_X_ORIGINAL_HOST'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment