Skip to content

Bootmanager

Joshua Parker edited this page Aug 22, 2020 · 4 revisions
<?php

namespace Rules;

use Qubus\Router\Router;
use Qubus\Router\Http\Request;
use Laminas\Diactoros\Response;
use Qubus\Router\Interfaces\RouterBootManagerInterface;

class CustomRouterRules implements RouterBootManagerInterface
{

    /**
     * Called when router is booting and before the routes is loaded.
     *
     * @param \Qubus\Router\Router $router
     * @param \Qubus\Router\Http\Request $request
     */
    public function boot(Router $router, Request $request, Response $response)
    {
        $redirectRules = [
            '/article/view/1/' => '/hello-world/'
        ];

        foreach ($redirectRules as $url => $rule) {
            // If router is installed in a subdirectory, then retrieve the basepath.
            $basepath = rtrim($router->getBasePath(), '/');
            // If the current url matches the rewrite url, we use our custom route
            if ($request->getUrl()->getPath() === $basepath . $url) {
                $router->redirect($url, $basepath . $rule);
            }
        }
    }
}
Clone this wiki locally