Welcome Guest, Not a member yet? Register   Sign In
Protect helpers and setup db connection for helpers
#1

(This post was last modified: 12-23-2024, 06:35 AM by 4usol.)

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();
..
}

..."
Reply
#2

(This post was last modified: 12-24-2024, 04:07 AM by InsiteFX.)

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 )
Reply
#3

(This post was last modified: 12-24-2024, 03:13 AM by 4usol.)

(12-23-2024, 10:22 PM)InsiteFX Wrote: 1) No, you do not need that line in the top of CI 4.
2) Database connection yesyou need to intialize it in a helper, but here is an easier way.

PHP Code:
$db db_connect(); 

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?
Reply
#4

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 )
Reply
#5

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)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);


        //Create global instance for DB-Connections
        $globalDB = db_connect('globalDB');
        //$modDB = db_connect('second_db');
        //$archivDB = db_connect('second_db');

        // Preload any models, libraries, etc, here.

        // E.g.: $this->session = \Config\Services::session();
    }"

in my modell, i define nothingelse, i trie to do this:
"
Code:
public function users_get_by_id(int $id, array $fields=array())
    {            
            return $globalDB->select($fields)->where('id', $id)->asArray();      
    }
"

The result is: "Undefined variable $globalDB"

Is it possible to define the db only in one file? If, what i do wrong?
Reply
#6

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 )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB