Skip to content

Commit

Permalink
add helper for current app url with port
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 14, 2022
1 parent 991b787 commit 5702317
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion config/sanctum.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Laravel\Sanctum\Sanctum;

return [

/*
Expand All @@ -16,7 +18,7 @@
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
'%s%s',
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
env('APP_URL') ? ','.parse_url(env('APP_URL'), PHP_URL_HOST) : ''
Sanctum::currentApplicationUrlWithPort(),
))),

/*
Expand Down
10 changes: 10 additions & 0 deletions src/Sanctum.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ class Sanctum
*/
public static $runsMigrations = true;

/**
* Get the current application URL from the "APP_URL" environment variable - with port.
*
* @return string
*/
public static function currentApplicationUrlWithPort()
{
return env('APP_URL') ? ','.parse_url(env('APP_URL'), PHP_URL_HOST).(parse_url(env('APP_URL'), PHP_URL_PORT) ? ':'.parse_url(env('APP_URL'), PHP_URL_PORT) : '') : '';
}

/**
* Set the current user for the application with the given abilities.
*
Expand Down

3 comments on commit 5702317

@suyar
Copy link
Contributor

@suyar suyar commented on 5702317 Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I should use the config function instead of the env function, because if I call Sanctum::currentApplicationUrlWithPort() elsewhere, the env function will not fetch the value in the production environment.

In a production environment, I usually run php artisan config:cache, but this causes the env function to get a null value.

@driesvints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@suyar we're gonna revert that. Thanks!

@driesvints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to leave that in please. There doesn't seems to be any breaking change.

Please sign in to comment.