CodeIgniter Forums

Full Version: How to make routes case insensitive
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've create API on CI4 and set all the routes then found out that all routes are case sensitive though I want to ignore case and match either way, how do I do it?

This should be routed with 
www.mydomain.com/api
www.mydomain.com/Api
$routes->group('api'function ($routes) {
    $routes->post('/''MyController::myFunction');
});
Never mind, I've implemented a function on $routes to ignore case;

$routes->setIgnoreCaseSensitive(true);
Be nice if you showed your code for the rest of the forum users.
In system/Router/RouteCollection.php on Line no: 84  
After protected $autoRoute = trueAdd>
    /**
     * Whether to ignore case sensitive in URI.
     * 
     * Not used here. Pass-thru value for Router class.
     *
     * @var boolean
     */
    protected $ignoreCase = false;


In system/Router/RouteCollection.php on Line no: 335
After public function setAutoRoute(bool $value) {...} Add>

    /**
     * Tells the system whether to ignore case in URI strings.
     *
     * @param boolean $value
     *
     * @return RouteCollectionInterface
     */
    public function setIgnoreCaseSensitive(bool $value): RouteCollectionInterface
    {
        $this->ignoreCase = $value;

        return $this;
    }

    //--------------------------------------------------------------------

In system/Router/RouteCollection.php on Line no: 484
After public function shouldAutoRoute() {...} Add>
 /**
     * Returns the flag that tells whether to ignore case sensitivity in URI.
     *
     * @return boolean
     */
    public function shouldIgnoreCaseSensitive(): bool
    {
        return $this->ignoreCase;
    }

    //--------------------------------------------------------------------


In system/Router/RouteCollectionInterface.php on Line no: 124

After public function setAutoRoute(bool $value): self; = trueAdd>
    /**
     * Tells the system whether to ignore case in URI strings.
     *
     * @param boolean $value
     *
     * @return RouteCollectionInterface
     */
    public function setIgnoreCaseSensitive(bool $value): self;

    //--------------------------------------------------------------------



In system/Router/RouteCollectionInterface.php on Line no: 176
After public function shouldTranslateURIDashes(); Add>
    /**
     * Returns the flag that tells whether to ignore case sensitivity in URI.
     *
     * @return boolean
     */
    public function shouldIgnoreCaseSensitive();

    //--------------------------------------------------------------------

In system/Router/Router.php on Line no: 407
After if (strpos($key, '{locale}'!== false){...} Add> 
 if ($this->collection->shouldIgnoreCaseSensitive() === true) {
                $uri = strtolower($uri);
                $key = strtolower($key);
            }



In app/Config/Routes.php
Add anywhere>
$routes->setIgnoreCaseSensitive(true);


If you don't want to make changes then I've also attached the files just download and place them at system/Router/<downloaded files>
[attachment=2009]
[attachment=2010]
[attachment=2011]
@UzairAli001,

I trap case insensitive URLs in Controllers:

Code:
# ==========================================
public function urls($url='')
{
   // BaseController -> data['urls'];
   // $this->data['urls'] = case sensitive array of permitted URLs

   if( in_array($url, $this->data['urls']) ) :
      return view('v_subpage/'.$url .'/index', $this->data);

   elseif( empty($url) ) :   
      return view('v_homepage', $this->data);

   else:
      $this->data['title'] = $this->data['title'] . ' DOES NOT EXIST???';
      $this->data['menu']  = view('incs/_menu', $this->data);

      return view('v_subpage', $this->data);
   endif;   
}//