![]() |
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(); .. } ..."
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(); What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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?
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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?
CodeIgniter 4 Users Guide - Connecting to Multiple Databases
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |