![]() |
Call BaseController custom function in view - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Call BaseController custom function in view (/showthread.php?tid=76185) Pages:
1
2
|
Call BaseController custom function in view - nicojmb - 04-20-2020 Hi, I've created a custom function in BaseController an i need to call inside a view but I can't find how. I would like to to create a "helper" and inside a view call it, buy i don't know access to BaseController variable in helper file. Please helpme. Regards! RE: Call BaseController custom function in view - 5flex - 04-20-2020 BaseController is a common base for expanding your other controllers. However, it is the same Controller as the rest. Everything that you call in your controllers can be called in it in the same way. If not difficult, can you show your code? RE: Call BaseController custom function in view - nicojmb - 04-20-2020 (04-20-2020, 06:01 AM)5flex Wrote: BaseController is a common base for expanding your other controllers. However, it is the same Controller as the rest. Everything that you call in your controllers can be called in it in the same way. If not difficult, can you show your code? Hi, i need to call inside a view a custom function created in BaseController. BaseController: PHP Code: <?php View: PHP Code: <?php RE: Call BaseController custom function in view - 5flex - 04-20-2020 How you can try to call the function if it has PRIVATE modificator? Change private on public or protected! And further. You cannot access your BaseController directly. You must create your controller and extend it (through extends) from BaseController! RE: Call BaseController custom function in view - nicojmb - 04-20-2020 (04-20-2020, 06:55 AM)5flex Wrote: How you can try to call the function if it has PRIVATE modificator? Change private on public or protected! It's a typographic error writing this post, the function is public: PHP Code: <?php In view: PHP Code: <?php RE: Call BaseController custom function in view - 5flex - 04-20-2020 For this implementation you must use view_cells(), because $this-> in Views it inctance of Vews but not the Controller. https://codeigniter.com/user_guide/outgoing/view_cells.html RE: Call BaseController custom function in view - kilishan - 04-20-2020 Or run the custom function within your controller, assigning the output to a variable which is then passed to the view. That's more of a standard practice. RE: Call BaseController custom function in view - nicojmb - 04-20-2020 (04-20-2020, 08:37 AM)kilishan Wrote: Or run the custom function within your controller, assigning the output to a variable which is then passed to the view. That's more of a standard practice. mmm, mi real idea is, in view add assets with "section" function, inside this section make a function to prepend path and append file version. And BaseController (frontend): PHP Code: <?php BaseController (backend): PHP Code: <?php Layout View: PHP Code: <!doctype html> Controller View: PHP Code: <?= $this->extend('layout'); ?> RE: Call BaseController custom function in view - kilishan - 04-20-2020 You could make that a helper, and include it in your BaseController, then it's available within all views. Just a note: The $this context refers to the sytsem's View class, not the controller. RE: Call BaseController custom function in view - nicojmb - 04-20-2020 (04-20-2020, 09:23 AM)kilishan Wrote: You could make that a helper, and include it in your BaseController, then it's available within all views. This is my real idea, but how can access $assets variable defined in "BaseController"? Is there where base folder is configured. |