CodeIgniter Forums
How to use $request->uri->getSegment() inside view? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: How to use $request->uri->getSegment() inside view? (/showthread.php?tid=76793)



How to use $request->uri->getSegment() inside view? - 0x0undefined - 06-20-2020

I'm trying to use `$ request-> uri-> getSegment (1)` in the view but I get an error saying "Undefined variable: request", what should I do to display a given segment in the view?


RE: How to use $request->uri->getSegment() inside view? - wdeda - 06-20-2020

(06-20-2020, 11:02 AM)0x0undefined Wrote: I'm trying to use `$ request-> uri-> getSegment (1)` in the view but I get an error saying "Undefined variable: request", what should I do to display a given segment in the view?

I use this, in Model:
PHP Code:
$request = \Config\Services::request();
        $uri $request->uri;
        $id $uri->getSegment(3); 



RE: How to use $request->uri->getSegment() inside view? - luisbeltranh - 01-21-2021

(06-20-2020, 11:02 AM)0x0undefined Wrote: I'm trying to use `$ request-> uri-> getSegment (1)` in the view but I get an error saying "Undefined variable: request", what should I do to display a given segment in the view?
Did you solve the problem?


RE: How to use $request->uri->getSegment() inside view? - LuxesR - 06-29-2021

(06-20-2020, 11:02 AM)0x0undefined Wrote: I'm trying to use `$ request-> uri-> getSegment (1)` in the view but I get an error saying "Undefined variable: request", what should I do to display a given segment in the view?

Is there an answer for his problem? I have the same thing.


RE: How to use $request->uri->getSegment() inside view? - wdeda - 06-29-2021

PHP Code:
$request service('request');
$uri $request->uri;
$something $uri->getSegment(1); 



RE: How to use $request->uri->getSegment() inside view? - includebeer - 06-29-2021

This would work, but if you follow the MVC pattern, the view is only supposed to display data. It's not supposed to fetch all kind of data from the request or the database. If you need something in the view, the best practice is to get it in the controller and pass it to the view. This way, the view contains almost exclusively html code with the exception of the echoes for the data.