How to use public variables from controller in views in CI 4 |
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.
Pass the variable to the view. There’s no more global object in CI4 like there was in CI3.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
@ciddict,
Other untried methods would be to define a constant or to set a session variable. Edit: Tried and both options work OK. PHP Code: $view = \Config\Services::renderer(); https://codeigniter.com/user_guide/outgo...ta#setData https://codeigniter.com/user_guide/outgo...ht=setdata
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>
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?
You can use a library and the viewCells
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
@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 {
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
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 |