How to 301 redirect old URL to new URL in CakePHP 2.x with slug lookup?

I need to redirect (301) urls like this:

https://website.com/products/view/id

to:

https://website.com/products/product-name-slug

I know how to make the slug version of urls work, but I do not want to disable old ones completely, instead I want them to 301-redirect to its slug version. I don’t want them both working simultaneously as it is treated as duplicated content by Google SEO.

I also do not want to write redirect for every product in routes.php like this:

Router::redirect( '/product/view/1001', [ 'controller' => 'products', 'action' => 'view', 'product-1001-name-slug' ] );

Instead I would rather have dynamic lookup function to do it for me.

How can I do it in CakePHP 2.x?

solution: https://stackoverflow.com/questions/48747523/how-to-301-redirect-old-url-to-new-url-in-cakephp-2-x-with-slug-lookup