How to use public variables from controller in views in CI 4 - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: How to use public variables from controller in views in CI 4 (/showthread.php?tid=78288) Pages:
1
2
|
How to use public variables from controller in views in CI 4 - ciddict - 12-29-2020 Here is my controller: PHP Code: namespace App\Controllers; I can't call it from the view file: in views/my_folder/index.php PHP Code: <?php Showing this error: Code: ErrorException But in CI3, I could easily use it. What's wrong here? I've to call the same thing everywhere mostly. So, I shouldn't pass the variable to view(). Isn't it? Please provide any solution. Thanks in advance. RE: How to use public variables from controller in views in CI 4 - includebeer - 12-29-2020 Pass the variable to the view. There’s no more global object in CI4 like there was in CI3. RE: How to use public variables from controller in views in CI 4 - John_Betong - 12-29-2020 @ciddict, Other untried methods would be to define a constant or to set a session variable. Edit: Tried and both options work OK. RE: How to use public variables from controller in views in CI 4 - MIS - 12-29-2020 PHP Code: $view = \Config\Services::renderer(); https://codeigniter.com/user_guide/outgoing/view_parser.html?highlight=setdata#setData https://codeigniter.com/user_guide/outgoing/view_parser.html?highlight=setdata RE: How to use public variables from controller in views in CI 4 - adnzaki - 12-29-2020 There is no way to call any variables outside of view() method. You have to pass them into view() so it can reads your data. If you have a property in your class, you have to pass it to data array in order to make it callable in view. Consider writing this code: PHP Code: <?php namespace App\Controllers; PHP Code: <div> RE: How to use public variables from controller in views in CI 4 - ciddict - 12-29-2020 Thanks for the answers. But this gonna be a mess for me. Could you guys anyone please provide me any special trick to perform this? RE: How to use public variables from controller in views in CI 4 - InsiteFX - 12-30-2020 You can use a library and the viewCells RE: How to use public variables from controller in views in CI 4 - ciddict - 12-30-2020 (12-30-2020, 12:57 AM)InsiteFX Wrote: You can use a library and the viewCells Even with this, I'll have to modify all the existing usages... RE: How to use public variables from controller in views in CI 4 - John_Betong - 12-30-2020 @ciddict, Did you try the two suggestions I made in post #3? I did, both work and either one could solve your problems. Edit: I just re-checked the original post and also advise to extend base_controller instead of each controller: > class MY_Controller extends Base_Controller { RE: How to use public variables from controller in views in CI 4 - includebeer - 12-30-2020 Like John_Betong suggested, extend the base controller. What I like to do is something like this: PHP Code: class BaseController extends Controller Then in my controllers I add some other data specific to that controller/method: PHP Code: class SomeController extends BaseController |