![]() |
Protect helpers and setup db connection for helpers - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11) +--- Thread: Protect helpers and setup db connection for helpers (/showthread.php?tid=92214) |
Protect helpers and setup db connection for helpers - 4usol - 12-23-2024 Hi, in ci3 i see this line at top of my helpers "if ( ! defined('BASEPATH')) exit('No direct script access allowed');" is it recommend to use it in ci4 too? By the way, do i need to initialise the db-connection on every helper function again, or is there a better way to support db connection in the helper functions i need? "my_helperfunction($this){ $db = \Config\Database::connect(); .. } my_helperfunction_next($this){ $db = \Config\Database::connect(); .. } ..." RE: Protect helpers and setup db connection for helpers - InsiteFX - 12-23-2024 1) No, you do not need that line in the top of CI 4. 2) Database connection yes you need to initialize it in a helper, but here is an easier way. PHP Code: $db = db_connect(); RE: Protect helpers and setup db connection for helpers - 4usol - 12-24-2024 (12-23-2024, 10:22 PM)InsiteFX Wrote: 1) No, you do not need that line in the top of CI 4. Thanks a lot, have i intitialize it in every single function of the helper again and again or in a kind of outside the functions in gloabl scope of the helper or something else? RE: Protect helpers and setup db connection for helpers - InsiteFX - 12-24-2024 If you place all your methods in a class then you can initialize the database in the class __construct method. In a function you would need to initialize it in every single function/method. RE: Protect helpers and setup db connection for helpers - 4usol - 12-26-2024 Maybe i'm at the wrong road, but, i try to setup my database connection in the base_controller. I want to declare/setup the db-connection in best case only one time, in one file. in basecontroller i do that Code: " public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) in my modell, i define nothingelse, i trie to do this: " Code: public function users_get_by_id(int $id, array $fields=array()) The result is: "Undefined variable $globalDB" Is it possible to define the db only in one file? If, what i do wrong? RE: Protect helpers and setup db connection for helpers - InsiteFX - 12-29-2024 CodeIgniter 4 Users Guide - Connecting to Multiple Databases |