├── .gitattributes ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── Router └── SpecialRouter.php ├── composer.json ├── etc ├── frontend │ └── di.xml └── module.xml └── registration.php /.gitattributes: -------------------------------------------------------------------------------- 1 | /docs export-ignore 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [2.0.0] - 2024-04-13 8 | ### Updated 9 | - Refactored router to implement RouterInterface [PR #4](https://github.com/markshust/magento2-module-specialrouter/pull/4). 10 | 11 | ## [1.0.1] - 2024-04-12 12 | ### Updated 13 | - Set router sort order to 25 to precede standard router processing [PR #1](https://github.com/markshust/magento2-module-specialrouter/pull/1). 14 | - Updated code for best practices and coding standards [PR #2](https://github.com/markshust/magento2-module-specialrouter/pull/2). 15 | 16 | ### Added 17 | - Add security-advisories package [PR #3](https://github.com/markshust/magento2-module-specialrouter/pull/3). 18 | 19 | ## [1.0.0] - 2022-12-16 20 | - Initial release. 21 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

MarkShust_SpecialRouter

2 | 3 |
4 |

Adds the ability to use special characters in URLs.

5 | Supported Magento Versions 6 | Latest Stable Version 7 | Composer Downloads 8 | Maintained - Yes 9 | 10 |
11 | 12 | ## Table of contents 13 | 14 | - [Summary](#summary) 15 | - [Installation](#installation) 16 | - [Usage](#usage) 17 | - [Credits](#credits) 18 | - [License](#license) 19 | 20 | ## Summary 21 | 22 | Magento does not provide the ability to set special characters on controller or action names. This module fixes that by providing the ability to use `-`, `.`, `~`, `_` within a URL. 23 | 24 | ## Installation 25 | 26 | ``` 27 | composer require markshust/magento2-module-specialrouter 28 | bin/magento module:enable MarkShust_SpecialRouter 29 | bin/magento setup:upgrade 30 | ``` 31 | 32 | ## Usage 33 | 34 | This module is really simple to use. All you need to do is use the appropriate "name" that matches the symbol you'd like to use in the URL. 35 | 36 | - Symbol: `-`, Name: `Dash` 37 | - Symbol: `.`, Name: `Period` 38 | - Symbol: `~`, Name: `Tilda` 39 | - Symbol: `_`, Name: `Underscore` 40 | 41 | For example, to respond to a request with a `frontName` of `foo` at the following location: 42 | 43 | ``` 44 | /foo/alpha-beta/charlie-delta 45 | ``` 46 | 47 | Use a PHP class named: 48 | 49 | ``` 50 | Controller/AlphaDashBeta/CharlieDashDelta.php 51 | ``` 52 | 53 | The `-` in the URL will be translated to `dash` in the actionPath and actionName, so if we create files using `Dash` in the controller and action name, they will respond to these requests. 54 | 55 | ## Credits 56 | 57 | ### M.academy 58 | 59 | This course is sponsored by M.academy, the simplest way to learn Magento. 60 | 61 | M.academy 62 | 63 | ### Mark Shust 64 | 65 | My name is Mark Shust and I'm the creator of this repo. I'm a 6X Adobe Commerce Certified Developer and have been involved with Magento since the early days (v0.8!). I create technical education courses full-time for my company, M.academy. 66 | 67 | - 🖥️ See my Magento lessons & courses 68 | - 📖 Read my technical articles 69 | - 🎥 Watch my YouTube videos 70 | - 🔗 Connect on LinkedIn 71 | - 🐦 Follow me on X 72 | - 💌 Contact me 73 | 74 | ## License 75 | 76 | [MIT](https://opensource.org/licenses/MIT) 77 | -------------------------------------------------------------------------------- /Router/SpecialRouter.php: -------------------------------------------------------------------------------- 1 | '-', 15 | 'name' => 'dash', 16 | ], 17 | [ 18 | 'symbol' => '.', 19 | 'name' => 'period', 20 | ], 21 | [ 22 | 'symbol' => '~', 23 | 'name' => 'tilda', 24 | ], 25 | [ 26 | 'symbol' => '_', 27 | 'name' => 'underscore', 28 | ], 29 | ]; 30 | 31 | /** 32 | * Match a route to this router. 33 | * 34 | * If there is a match, replace the path with the new path. 35 | * Ex. /front-name/foo-bar/baz_qux -> /front-name/foodashbar/bazunderscorequx 36 | * 37 | * @param RequestInterface $request 38 | * @return void 39 | */ 40 | public function match( 41 | RequestInterface $request, 42 | ): void { 43 | $identifier = trim($request->getPathInfo(), '/'); 44 | $pathParts = explode('/', $identifier); 45 | $moduleName = array_shift($pathParts); 46 | $pathInfo = implode('/', $pathParts); 47 | 48 | if ($this->isMatch($pathInfo)) { 49 | $newPathInfo = sprintf('/%s/%s', $moduleName, $this->replacePath($pathInfo)); 50 | $request->setPathInfo($newPathInfo); 51 | } 52 | 53 | // Return void to allow the next router to match (void vs. null for PHP <=8.1 compatibility). 54 | } 55 | 56 | /** 57 | * Does the path contain a character in the symbol to name map? 58 | * 59 | * @param string $pathInfo 60 | * @return bool 61 | */ 62 | private function isMatch( 63 | string $pathInfo, 64 | ): bool { 65 | foreach (self::SYMBOL_TO_NAME_MAP as $item) { 66 | if (str_contains($pathInfo, $item['symbol'])) { 67 | return true; 68 | } 69 | } 70 | 71 | return false; 72 | } 73 | 74 | /** 75 | * Replace special characters in the path with their names. 76 | * 77 | * @param string $pathInfo 78 | * @return string 79 | */ 80 | private function replacePath( 81 | string $pathInfo, 82 | ): string { 83 | foreach (self::SYMBOL_TO_NAME_MAP as $item) { 84 | $pathInfo = str_replace($item['symbol'], $item['name'], $pathInfo); 85 | } 86 | 87 | return $pathInfo; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "markshust/magento2-module-specialrouter", 3 | "description": "The SpecialRouter module adds the ability to use special characters in URLs.", 4 | "require": { 5 | "php": "^8", 6 | "magento/framework": ">=103" 7 | }, 8 | "require-dev": { 9 | "roave/security-advisories": "dev-latest" 10 | }, 11 | "type": "magento2-module", 12 | "version": "2.0.0", 13 | "license": [ 14 | "MIT" 15 | ], 16 | "autoload": { 17 | "files": [ 18 | "registration.php" 19 | ], 20 | "psr-4": { 21 | "MarkShust\\SpecialRouter\\": "" 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /etc/frontend/di.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | MarkShust\SpecialRouter\Router\SpecialRouter 8 | 25 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /etc/module.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /registration.php: -------------------------------------------------------------------------------- 1 |