CodeIgniter Forums
How to make routes case insensitive - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: Issues (https://forum.codeigniter.com/forum-19.html)
+--- Thread: How to make routes case insensitive (/thread-78910.html)



How to make routes case insensitive - UzairAli001 - 03-26-2021

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');
});


RE: How to make routes case insensitive - UzairAli001 - 03-27-2021

Never mind, I've implemented a function on $routes to ignore case;

$routes->setIgnoreCaseSensitive(true);


RE: How to make routes case insensitive - InsiteFX - 03-28-2021

Be nice if you showed your code for the rest of the forum users.


RE: How to make routes case insensitive - UzairAli001 - 03-28-2021

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]


RE: How to make routes case insensitive - John_Betong - 03-28-2021

@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;   
}//