Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
rework: URI creation and URL helper#7282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
d01c783
refactor: current URI creation
kenjis fc0a1b8
refactor: URL helper site_url(), base_url(), current_url(), uri_string()
kenjis 468b8b5
test: fix incorrect REQUEST_URI
kenjis c29c172
test: update FormHelperTest
kenjis e68379e
test: fix failed test
kenjis 8464087
test: update URLs in assertions
kenjis 76dfd39
feat: add Services::siteurifactory()
kenjis 0674874
refactor: remove adjustPathTrailingSlash()
kenjis de46d3e
fix: SiteURI's query is not set in FeatureTestTrait
kenjis 9827e0e
fix: use SiteURI in ControllerTestTrait
kenjis 3207842
test: update assertion URL (add `index.php/`)
kenjis a4ba66d
refactor: remove unused private method
kenjis 9ec7fbc
feat: add Services::superglobals()
kenjis f00cd8c
fix: out-of-dated parameters
kenjis 7017919
test: use Services::superglobals()
kenjis 715bd00
test: update out-of-dated parameters
kenjis f24ca6a
docs: add doc comments
kenjis 7514d6d
fix: support protocol-relative links
kenjis ae4f7c8
docs: add @deprecated versions
kenjis 6c83470
docs: update user guide
kenjis e4a2523
docs: add @deprecated versions
kenjis c2f8860
docs: add Deprecations
kenjis e01d622
test: remove unused private method
kenjis eeca9e9
docs: fix section level
kenjis 9752815
test: update data provider method name
kenjis d80c7b5
fix: ControllerTestTrait::withUri() updates Request instance
kenjis 3a76189
test: update data provider method name
kenjis 0eebc5e
test: add tests for SiteURIFactory
kenjis d9a5a6d
fix: when URI string does not have path
kenjis 546d112
test: remove redundant test
kenjis 16621bf
test: add tests for uri_string() and current_url()
kenjis cce185c
docs: add upgrade_440
kenjis f35dc9b
docs: fix typo
kenjis c6f3d57
refactor: use Services::superglobals()
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -145,7 +145,7 @@ private function determineBaseURL( | ||
| $uri = new URI($baseURL); | ||
| // Update scheme | ||
| if ($scheme !== null) { | ||
| if ($scheme !== null && $scheme !== '') { | ||
| $uri->setScheme($scheme); | ||
| } elseif ($configApp->forceGlobalSecureRequests) { | ||
| $uri->setScheme('https'); | ||
| @@ -363,4 +363,67 @@ protected function applyParts(array $parts): void | ||
| $this->password = $parts['pass']; | ||
| } | ||
| } | ||
| /** | ||
| * For base_url() helper. | ||
| * | ||
| * @param array|string $relativePath URI string or array of URI segments | ||
| * @param string|null $scheme URI scheme. E.g., http, ftp | ||
| */ | ||
| public function baseUrl($relativePath = '', ?string $scheme = null): string | ||
| { | ||
| $relativePath = $this->stringifyRelativePath($relativePath); | ||
| $config = clone config(App::class); | ||
| $config->indexPage = ''; | ||
| $host = $this->getHost(); | ||
| $uri = new self($config, $relativePath, $host, $scheme); | ||
| // Support protocol-relative links | ||
| if ($scheme === '') { | ||
MGatner marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return substr((string) $uri, strlen($uri->getScheme()) + 1); | ||
| } | ||
| return (string) $uri; | ||
| } | ||
| /** | ||
| * @param array|string $relativePath URI string or array of URI segments | ||
| */ | ||
| private function stringifyRelativePath($relativePath): string | ||
| { | ||
| if (is_array($relativePath)) { | ||
| $relativePath = implode('/', $relativePath); | ||
| } | ||
| return $relativePath; | ||
| } | ||
| /** | ||
| * For site_url() helper. | ||
| * | ||
| * @param array|string $relativePath URI string or array of URI segments | ||
| * @param string|null $scheme URI scheme. E.g., http, ftp | ||
| * @param App|null $config Alternate configuration to use | ||
| */ | ||
| public function siteUrl($relativePath = '', ?string $scheme = null, ?App $config = null): string | ||
| { | ||
| $relativePath = $this->stringifyRelativePath($relativePath); | ||
| // Check current host. | ||
| $host = $config === null ? $this->getHost() : null; | ||
| $config ??= config(App::class); | ||
| $uri = new self($config, $relativePath, $host, $scheme); | ||
| // Support protocol-relative links | ||
| if ($scheme === '') { | ||
| return substr((string) $uri, strlen($uri->getScheme()) + 1); | ||
| } | ||
| return (string) $uri; | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,6 +15,11 @@ | ||
| use CodeIgniter\Superglobals; | ||
| use Config\App; | ||
| /** | ||
| * Creates SiteURI using superglobals. | ||
| * | ||
| * This class also updates superglobal $_SERVER and $_GET. | ||
| */ | ||
| final class SiteURIFactory | ||
| { | ||
| private App $appConfig; | ||
| @@ -42,6 +47,7 @@ public function createFromGlobals(): SiteURI | ||
| * Create the SiteURI object from URI string. | ||
| * | ||
| * @internal Used for testing purposes only. | ||
| * @testTag | ||
MGatner marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| */ | ||
| public function createFromString(string $uri): SiteURI | ||
| { | ||
| @@ -64,7 +70,7 @@ public function createFromString(string $uri): SiteURI | ||
| $fragment = '#' . $parts['fragment']; | ||
| } | ||
| $relativePath = $parts['path'] . $query . $fragment; | ||
| $relativePath = ($parts['path'] ?? '') . $query . $fragment; | ||
| $host = $this->getValidHost($parts['host']); | ||
| return new SiteURI($this->appConfig, $relativePath, $host, $parts['scheme']); | ||
| @@ -79,6 +85,7 @@ public function createFromString(string $uri): SiteURI | ||
| * @return string The route path | ||
| * | ||
| * @internal Used for testing purposes only. | ||
| * @testTag | ||
| */ | ||
| public function detectRoutePath(string $protocol = ''): string | ||
| { | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.