Welcome Guest, Not a member yet? Register   Sign In
getting array of uri segments excluding subdirectory in base_url()
#1

I am trying to find an optimal way to get an array of segments of the uri of my application that would not include the subdirectory where my app resides, if I use such (I do in production, but not in the development env).
Previously I have used code that would check the first segment against a keyword, and if it matches (which means my base_url includes a subdirectory), the first segment gets ignored:
PHP Code:
$uri current_url(true);
$segment_arr = [];
if (
$uri->getTotalSegments() >= 1) {
   if (substr($uri->getSegment(1), 07) == 'sistema'//the check for subdir
     $i 2;
   else $i 1;
   for ($i$i <= $uri->getTotalSegments(); $i++) {
     $segment_arr[] = $uri->getSegment($i);
   }

The resulting $segment_arr array includes the segment array.
It is cumbersome and depends on the name of the directory, which might change between projects, so I was looking for alternative. Was playing with this code:
PHP Code:
$uri trim(uri_string(false),'/');
$segment_arr = (!empty($uri)) ? explode('/',$uri) : []; 
The resulting array includes all the segments, if there are any. Seems to be simple enough and works independent of the app subdirectory (which is ignored by the uri_string() function).
However, I wonder if there are any caveats to my new approach. Any ideas? Or maybe there is a better, codeigniter'ish, way of doing it?

}
     
==

Donatas G.
Reply
#2

Why not use:

PHP Code:
$segments $uri->getSegments(); 
Reply
#3

(05-02-2022, 11:59 PM)JustJohnQ Wrote: Why not use:

PHP Code:
$segments $uri->getSegments(); 

Well, because in case the url is https://mywebsite.com/appsubdir/firstseg/secondseg  it returns

Code:
Array
(
    [0] => appsubdir
    [1] => firstseg
    [2] => secondseg
)

while in my development environment, with url like http://localohost:8080/firstseg/secondseg (no app subdirectory)  it returns

Code:
Array
(
    [0] => firstseg
    [1] => secondseg
)

And I need a universal way, independent of the name of the app subdirectory or it's absence, to get the array:

Code:
Array
(
    [0] => firstseg
    [1] => secondseg
)
==

Donatas G.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB