Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Support for specify a domain in paths (#514)
Browse files Browse the repository at this point in the history
* Adding the ability to specify paths domain.

now you can specify domain in config paths, looks like

* update readme for domain support
  • Loading branch information
yojnc authored Feb 17, 2021
1 parent bab0cb9 commit 4f92f55
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ Otherwise you can disable CSRF for certain requests in `App\Http\Middleware\Veri

```php
protected $except = [
'api/*'
'api/*',
'sub.domain.zone' => [
'prefix/*'
],
];
```

Expand Down
23 changes: 21 additions & 2 deletions src/HandleCors.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ protected function shouldRun(Request $request): bool
/**
* The the path from the config, to see if the CORS Service should run
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function isMatchingPath(Request $request): bool
{
// Get the paths from the config or the middleware
$paths = $this->container['config']->get('cors.paths', []);
$paths = $this->getPathsByHost($request->getHost());

foreach ($paths as $path) {
if ($path !== '/') {
Expand All @@ -114,4 +114,23 @@ protected function isMatchingPath(Request $request): bool

return false;
}

/**
* Paths by given host or string values in config by default
*
* @param string $host
* @return array
*/
protected function getPathsByHost(string $host)
{
$paths = $this->container['config']->get('cors.paths', []);
// If where are paths by given host
if (isset($paths[$host])) {
return $paths[$host];
}
// Defaults
return array_filter($paths, function ($path) {
return is_string($path);
});
}
}

0 comments on commit 4f92f55

Please sign in to comment.