Skip to content

Instantly share code, notes, and snippets.

@minedun6
Created August 5, 2020 13:44
Show Gist options
  • Save minedun6/3194dba2a48a41404330c62c67dce667 to your computer and use it in GitHub Desktop.
Save minedun6/3194dba2a48a41404330c62c67dce667 to your computer and use it in GitHub Desktop.
Something I struggled with is that `to()` adds the $parameters to the path by default `success/verify_email` instead of `success?type=verify_email`. So I've slightly adjusted your example to match my needs. @devgummibeer
<?php
namespace App\Providers;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;
use Arr;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
app(UrlGenerator::class)->macro('frontend', function (string $uri, array $parameters = []): string {
$this->forceRootUrl(config('app.frontend_url'));
$path = parse_url($uri, PHP_URL_PATH);
$query = parse_url($uri, PHP_URL_QUERY);
parse_str($query, $query);
$query = Arr::query(
array_merge($query, $parameters)
);
return tap(
$this->to($path, ($query ? '?' . $query : '')),
fn => $this->forceRootUrl(null)
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment