Welcome Guest, Not a member yet? Register   Sign In
How to use $request->uri->getSegment() inside view?
#1
Exclamation 

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?
Reply
#2

(This post was last modified: 06-20-2020, 11:20 PM by wdeda.)

(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); 
Reply
#3

(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?
Reply
#4

(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.
Reply
#5

(This post was last modified: 06-29-2021, 01:46 PM by wdeda.)

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

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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB