Welcome Guest, Not a member yet? Register   Sign In
How to make routes case insensitive
#1
Brick 

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

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

$routes->setIgnoreCaseSensitive(true);
Reply
#3

Be nice if you showed your code for the rest of the forum users.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4
Wink 

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>

.php   RouteCollectionInterface.php (Size: 7.08 KB / Downloads: 5)


.php   Router.php (Size: 17.27 KB / Downloads: 5)


.php   RouteCollection.php (Size: 42.25 KB / Downloads: 5)
Reply
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB