-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 90cd78e
Showing
11 changed files
with
551 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: CI | ||
|
||
on: [ push ] | ||
|
||
jobs: | ||
phpunit: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: php-actions/composer@v6 | ||
- name: PHPUnit Tests | ||
uses: php-actions/phpunit@v4 | ||
with: | ||
php_extensions: xdebug | ||
bootstrap: vendor/autoload.php | ||
configuration: phpunit.xml | ||
coverage_text: true | ||
env: | ||
XDEBUG_MODE: coverage | ||
|
||
phpstan: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: php-actions/composer@v6 | ||
- uses: php-actions/phpstan@v3 | ||
with: | ||
version: latest | ||
path: Classes Tests | ||
configuration: phpstan.neon |
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.idea | ||
composer.lock | ||
vendor | ||
Packages | ||
.phpunit.result.cache | ||
bin | ||
Build |
This file contains 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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flowpack\SeoRouting; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\UriFactoryInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
final class RoutingMiddleware implements MiddlewareInterface | ||
{ | ||
#[Flow\Inject] | ||
protected ResponseFactoryInterface $responseFactory; | ||
|
||
#[Flow\Inject] | ||
protected UriFactoryInterface $uriFactory; | ||
|
||
/** @var array{enable: array{trailingSlash: bool, toLowerCase: bool}, statusCode?: int} */ | ||
#[Flow\InjectConfiguration(path: 'redirect')] | ||
protected array $configuration; | ||
|
||
/** @var array{string: bool} */ | ||
#[Flow\InjectConfiguration(path: 'blocklist')] | ||
protected array $blocklist; | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$isTrailingSlashEnabled = $this->configuration['enable']['trailingSlash'] ?? false; | ||
$isToLowerCaseEnabled = $this->configuration['enable']['toLowerCase'] ?? false; | ||
|
||
$uri = $request->getUri(); | ||
|
||
if (! $isTrailingSlashEnabled && ! $isToLowerCaseEnabled) { | ||
return $handler->handle($request); | ||
} | ||
|
||
if ($this->matchesBlocklist($uri)) { | ||
return $handler->handle($request); | ||
} | ||
|
||
$oldPath = $uri->getPath(); | ||
|
||
if ($isTrailingSlashEnabled) { | ||
$uri = $this->handleTrailingSlash($uri); | ||
} | ||
|
||
if ($isToLowerCaseEnabled) { | ||
$uri = $this->handleToLowerCase($uri); | ||
} | ||
|
||
if ($uri->getPath() === $oldPath) { | ||
return $handler->handle($request); | ||
} | ||
|
||
$response = $this->responseFactory->createResponse($this->configuration['statusCode'] ?? 301); | ||
|
||
return $response->withAddedHeader('Location', (string)$uri); | ||
} | ||
|
||
private function handleTrailingSlash(UriInterface $uri): UriInterface | ||
{ | ||
if (strlen($uri->getPath()) === 0) { | ||
return $uri; | ||
} | ||
|
||
if (array_key_exists('extension', pathinfo($uri->getPath()))) { | ||
return $uri; | ||
} | ||
|
||
return $uri->withPath(rtrim($uri->getPath(), '/') . '/') | ||
->withQuery($uri->getQuery()) | ||
->withFragment($uri->getFragment()); | ||
} | ||
|
||
private function handleToLowerCase(UriInterface $uri): UriInterface | ||
{ | ||
$loweredPath = strtolower($uri->getPath()); | ||
|
||
if ($uri->getPath() === $loweredPath) { | ||
return $uri; | ||
} | ||
|
||
$newUri = str_replace($uri->getPath(), $loweredPath, (string)$uri); | ||
|
||
return $this->uriFactory->createUri($newUri); | ||
} | ||
|
||
private function matchesBlocklist(UriInterface $uri): bool | ||
{ | ||
$path = $uri->getPath(); | ||
foreach ($this->blocklist as $rawPattern => $active) { | ||
$pattern = '/' . str_replace('/', '\/', $rawPattern) . '/'; | ||
|
||
if (! $active) { | ||
continue; | ||
} | ||
|
||
if (preg_match($pattern, $path) === 1) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'Neos.Neos:Document': | ||
properties: | ||
uriPathSegment: | ||
validation: | ||
'Neos.Neos/Validation/RegularExpressionValidator': | ||
regularExpression: '/^[a-z0-9\-]+$/' #override original regex (removes insensitive) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Flowpack: | ||
SeoRouting: | ||
redirect: | ||
enable: | ||
trailingSlash: true | ||
toLowerCase: false | ||
statusCode: 301 | ||
blocklist: | ||
'/neos.*': true | ||
|
||
Neos: | ||
Flow: | ||
http: | ||
middlewares: | ||
'before routing': | ||
middleware: 'Flowpack\SeoRouting\RoutingMiddleware' |
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018-2025 yeebase media GmbH, Sandstorm Media GmbH | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Flowpack.SeoRouting | ||
|
||
<!-- TOC --> | ||
|
||
* [Flowpack.SeoRouting](#flowpackseorouting) | ||
* [Introduction](#introduction) | ||
* [Features](#features) | ||
* [Installation](#installation) | ||
* [Configuration](#configuration) | ||
* [Standard Configuration](#standard-configuration) | ||
* [Blocklist for redirects](#blocklist-for-redirects) | ||
* [Thank you](#thank-you) | ||
|
||
<!-- TOC --> | ||
|
||
## Introduction | ||
|
||
This package allows you to enforce a trailing slash and/or lower case urls in Flow/Neos. | ||
|
||
## Features | ||
|
||
This package has 2 main features: | ||
|
||
- **trailingSlash**: ensure that all links ends with a trailing slash (e.g. `example.com/test/` instead of | ||
`example.com/test`) | ||
- **toLowerCase**: ensure that camelCase links gets redirected to lowercase (e.g. `example.com/lowercase` instead of | ||
`example.com/lowerCase`) | ||
|
||
You can de- and activate both of them. | ||
|
||
Another small feature is to restrict all _new_ neos pages to have a lowercased `uriPathSegment`. This is done by | ||
extending the `NodeTypes.Document.yaml`. | ||
|
||
## Installation | ||
|
||
Just require it via composer: | ||
|
||
`composer require flowpack/seo-routing` | ||
|
||
## Configuration | ||
|
||
### Standard Configuration | ||
|
||
In the standard configuration we have activated the trailingSlash (to redirect all uris without a / at the end to an uri | ||
with / at the end) and do all redirects with a 301 http status. | ||
|
||
*Note: The lowercase redirect is deactivated by default, because you have to make sure, that there is | ||
no `uriPathSegment` | ||
with camelCase or upperspace letters - this would lead to redirects in the neverland.* | ||
|
||
``` | ||
Flowpack: | ||
SeoRouting: | ||
redirect: | ||
enable: | ||
trailingSlash: true | ||
toLowerCase: false | ||
statusCode: 301 | ||
blocklist: | ||
'/neos.*': true | ||
``` | ||
|
||
### Blocklist for redirects | ||
|
||
By default, all `/neos` URLs are ignored for redirects. You can extend the blocklist array with regex as you like: | ||
|
||
```yaml | ||
Flowpack: | ||
SeoRouting: | ||
blocklist: | ||
'/neos.*': true | ||
``` | ||
## Thank you | ||
This package originates from https://github.com/t3n/seo-routing. | ||
Thank you, T3N and associates for your work. |
Oops, something went wrong.