Last active
January 10, 2023 15:16
-
-
Save benjibee/4e315a7db9e870f244b6807a83b90d40 to your computer and use it in GitHub Desktop.
Use ngrok URLs in Laravel for all assets, generated URLs, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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