CodeIgniter Forums
Url segment in ci4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Url segment in ci4 (/showthread.php?tid=74755)



Url segment in ci4 - seunex - 11-02-2019

Hey guys how do we call itel segment in CI4 as we do $this->uri->segme t() in ci3.


RE: Url segment in ci4 - donpwinston - 11-02-2019

In a controller:

$segments = $this->request->uri->getSegments();

$segment = $this->request->uri->getSegment(1);


RE: Url segment in ci4 - InsiteFX - 11-02-2019

CodeIgniter 4 User Guide - URI Segments -> Working with URIs


RE: Url segment in ci4 - wdeda - 11-08-2019

English is not my native language.
Maybe I'm too old to understand, 70, but it's also proof that I try to keep an open mind to the new, which I suppose will come to ease, simplify, etc., but the question asked by @seunex remains unanswered for me. despite the goodwill of @donpwiston and @InsiteFX.

In CI3 it was enough to use as below, in a model:
$id = $this->uri->segment(3);
$query = $this->db
->select('*')
->from('movies')
->where('id', $id) ...

Now I have to use two more lines of code otherwise it will give error:
$request = \Config\Services::request();
$uri = $request->uri;

Of course I'm no expert, I'm just curious, but I hoped the lightness, simplicity and objectivity that always characterized CodeIgniter would be further enhanced.
I built a website, local, actually a catalog of my records and movies - about 5,000 records and over 1,500 movies - with detailed information that works perfectly. All my difficulty was with learning PHP that I haven't mastered yet but learned enough to achieve my goals. So I am believing that my knowledge of PHP is quite rudimentary given the difficulties I have been encountering in converting from CI3 to CI4. Of course it wouldn't be necessary since it perfectly meets the goals, but the challenge of the task is healthy for someone my age and I wouldn't want to give up.
Thanks for the time and attention.


RE: Url segment in ci4 - plaztic - 01-25-2020

You can make your own helper function to return segment value.

PHP Code:
if (!function_exists('getSegment'))
{

    /**
     * Returns segment value for given segment number or false.
     *
     * @param int $number The segment number for which we want to return the value of
     *
     * @return string|false
     */
    function getSegment(int $number)
    {
        $request = \Config\Services::request();

        if ($request->uri->getTotalSegments() >= $number && $request->uri->getSegment($number))
        {
            return $request->uri->getSegment($number);
        }
        else
        {
            return false;
        }
    }





RE: Url segment in ci4 - prikkles - 04-17-2020

(11-02-2019, 08:03 AM)donpwinston Wrote: In a controller:

$segment =  $this->request->uri->getSegment(1);

Thank you. This worked brilliantly.

I'm still relatively new to CI4, but the CI documentation does not explain segments well at all. The documentation says:

Quote:URI Segments

Each section of the path between the slashes is a single segment. The URI class provides a simple way to determine what the values of the segments are. The segments start at 1 being the furthest left of the path.

Code:
// URI = http://example.com/users/15/profile

// Prints '15'
if ($request->uri->getSegment(1) == 'users')
{
        echo $request->uri->getSegment(2);
}
 


But I cannot get the documentation's explanation working. The closest I got was an 'Out of range' error.  Huh


RE: Url segment in ci4 - Cathal - 09-01-2020

(11-02-2019, 08:03 AM)donpwinston Wrote: In a controller:

$segments = $this->request->uri->getSegments();

$segment =  $this->request->uri->getSegment(1);


I actually created an account just so I could thank this post - agree with the OP that the CI documentation is overly complicated & confusing


RE: Url segment in ci4 - demyr - 09-01-2020

In your controller you can use :

PHP Code:
$data['current_uri'] = $this->request->uri->getSegment(2); 


And in your view file you can use :

PHP Code:
<?php $uri current_url(true); ?>

<?php echo $uri->getSegment(4); ?>