Keeping website urls consistent on a CakePHP powered website
The middleware keeps the urls of a CakePHP powered website consistent by preventing, for example, an action in a given controller from being accessed in a way other than as defined in the routes used by the website.

Jul 18, 2022
3 minutes

Step 1
Download the UrlConsistenceMiddleware from Github and move it to /src/Middleware/UrlConsistenceMiddleware.php
Step 2
Open your src/Application.php and add the UrlConsistenceMiddleware to your middleware queue but make sure that it is placed immediately after the RoutingMiddleware.
Step 3
Optional - clear the cache. You can do this via the terminal. Bring up a terminal window, change directory to where you installed your CakePHP web app / site. And issue the following command: bin/cake cache clear_all. What this command will do is clear your cache.
Why would you want to install this on your site?
The point of the middleware is to prevent the same url (web address), as defined in the routes of the website, from being accessed using a different web address.
If you are already running a cakephp powered website, the chances are that you already know that by default, when first installed, the home page for the website is served by an action defined in the Pages controller. In the most modern version of the framework, the home page of the website is connected to the Pages controller in the following way:
return static function (RouteBuilder $routes) {
$routes->scope('/', function (RouteBuilder $builder) {
/*
* Here, we are connecting '/' (base path) to a controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, templates/Pages/home.php)...
*/
$builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$builder->connect('/pages/*', 'Pages::display');
$builder->fallbacks();
});
};
What that does is, makes the home page of your website accessible via the / (forward slash) url. This of course is great, the problem is that the same page can still be accessed using another url, in this case /pages/display/home. The middleware prevents this from happening, and even when the user visits the /pages/display/home version the user is redirected to /, and the web browse is made aware that the page has moved permanently. This prevents url based duplicated content.
Another example
Most cakephp powered websites have a location where the administrator of the website can login to manage the site. This is usually in the Users controller in a method called login, which is normally accessed via the url /users/login. In most cases, site admins prefer to use a shorter url version, in this case simply /login instead of /users/login. So, now the user can login to the website using either version of the url, this of course is not good. The middleware prevents this and makes sure that the correct routing version is used.
Installing the middleware should be straight forward, but if you need any help installing it feel free to drop us a line.