How to use $request->uri->getSegment() inside view? |
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?
(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();
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! ***/ |
Welcome Guest, Not a member yet? Register Sign In |