skip to Main Content

I am using Codeigniter 4 framework and I have a function for getting the URI segment like

    $bahasa = $request->uri->getSegment(1);
    $url = $request->uri->getSegment(2);
    $content = $request->uri->getSegment(3);

the code is working if the URL most like:
http://localhost:8080/segment1/segment2/segment3
but if I put the URL like http://localhost:8080/segment1/ the code is an error, the error is
CodeIgniterHTTPExceptionsHTTPException Request URI segment is out of range: 2

4

Answers


  1. Just use isset function to check either the segment is exists or not. Like bellow:

    $url = isset( $request->uri->getSegment(2) ) ? $request->uri->getSegment(2) : '';
    

    That’s it!

    Login or Signup to reply.
  2. you can use this to check availability of your segement request

    if($request->uri->getSegment(2) || $request->uri->getSegment(3)){
    //response if the request is available
    }else{
    //response if the request is not available
    }
    

    if you still confuse you can read this documentation :
    https://codeigniter.com/user_guide/libraries/uri.html#creating-uri-instances

    Login or Signup to reply.
  3. Solution

    Disable Throwing Exceptions

    By default, some methods of this class may throw an exception. If you
    want to disable it, you can set a special flag that will prevent
    throwing exceptions.

    <?php
    
    // Disable throwing exceptions
    $uri->setSilent();
    
    // Enable throwing exceptions (default)
    $uri->setSilent(false);
    

    Hence if your sample URI is http://localhost:8080/segment1/:

    // $bahasa will be set to 'segment1'.
    $bahasa = $request->uri->setSilent()->getSegment(1);
    // $url will be set to '' (empty string).
    $url = $request->setSilent()->uri->getSegment(2);
    // $content will be set to '' (empty string).
    $content = $request->setSilent()->uri->getSegment(3);
    

    Extra tip:

    You can also set a different default value for a particular segment by
    using the second parameter of the getSegment() method. The default
    is empty string. – URI Segments

    Login or Signup to reply.
  4. best way to handle local and server segment issue need to change getSegment function

    getSegment function you can find system/http/URI.php

    getSegment function will look like this by default

    public function getSegment(int $number, string $default = ''): string
        {
            // The segment should treat the array as 1-based for the user
            // but we still have to deal with a zero-based array.
            $number--;
    
            if ($number > count($this->segments) && ! $this->silent) {
                throw HTTPException::forURISegmentOutOfRange($number);
            }
    
            return $this->segments[$number] ?? $default;
        }
    

    need to change segment number like

     public function getSegment(int $number, string $default = ''): string
        {
            // The segment should treat the array as 1-based for the user
            // but we still have to deal with a zero-based array.
             $hostname= getenv('HTTP_HOST'); 
              if($hostname=='localhost'){
                $number--;
              }else{ 
    //live server 
                $number=$number-2;
              }
            if ($number > count($this->segments) && ! $this->silent) {
                throw HTTPException::forURISegmentOutOfRange($number);
            }
        
            return $this->segments[$number] ?? $default;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search