Welcome Guest, Not a member yet? Register   Sign In
Feature Request: Route API: routeMatch(...)

(This post was last modified: 11-28-2022, 02:07 AM by chez14. Edit Reason: Change the scenario explanation due to unexpected behavior of `{locale}` )

I want to create a controller to enforce the locale settings on the URL. My client's website doesn't currently support the new url with locale settings on it, so we need to somehow able to transition the move gracefully, due to SEO reasons™ + ongoing advertising campaign.

To do this, I need a Router API that able to check if certain string will hit a certain route.  I found something from `RouteCollection` class: `RouteCollection::getFiltersForRoute()` to trick CI4 to check if certain route exist by adding transparent filter to all route first. If it actually hits something, then the function will return the expected transparent filter, but this doesn't help for route that looked like this:


When I got `gallery/new-year-2022/theater` I can't just use that trick anymore, because the mentioned function just match the exact string.

Upon checking `Router` class I also found `Router::checkRoutes()`, the actual function that handles the route parsing. Unfortunately this function is not provided for public use: not public, and actually change the internal parameters.

Intended Use Case:

By moving the route matching function to a public function (in this case, let's name it `routeMatch()`), I can create a controller that accepts anything and redirect them to the localized one, only if the route are exists by checking the original url against route collection.

Here's a sample simple scenario:

Route Configuration
/ →redirect_to /{locale}/home
/{locale}, [priority=10] → I18nKit::redirectHelper
/{locale}/home, [priority=10] → Home::index
/{locale}/gallery, [priority=10] → Gallery::index
/{locale}/gallery/(:⁣segment), [priority=10] → Gallery::view/$1
/{locale}/gallery/(:⁣segment)/theater, [priority=10] → Gallery::presentAsTeather/$1
/(:any), [priority=90]→ I18nKit::redirectHelper

- Accessing `/gallery` will match with `/{locale}` (due to unexpected behavior of `{loale}`) and make `I18nKit::redirectHelper` check if `/en/gallery` exists. Because the route is registered, it redirects to that url.
- Accessing `/about` will have the same matched controller, but because the entered url doesn't match to any route, it will instead show HTTP 404.
- Accessing `/gallery/new-year-2022` will match with `/(:any)` route, and makes the kit checks for `/en/gallery/new-year-2022`. Because the route exists, it will redirects there.
- Accessing `/en/gallery/new-year-2022/theater` will do just fine (match with route that have the locale there).

I hope the use case explanation is clear enough. 

Additional mini note: The mentioned route configuration has invisible separator in the middle of ":" and "s" due to ":⁣s" got replaced as Confused emoji, and ":\s" doesn't actually do the escape.


I need to point out that due to unexpected behavior of `{locale}`, any url with single segment will match with `/{locale}`.

I think with exist website not locate, simple using session to save and translate for locate.

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams


Theme © iAndrew 2016 - Forum software by © MyBB