Welcome Guest, Not a member yet? Register   Sign In
Constructor Controller (SQLite User Defined Functions)
#1

[eluser]Veehmot[/eluser]
Hey! I need to know how can I call some logic for the application startup. Basically, what I'm trying to do is to create some user defined functions for SQlite, to handle REGEXP. I had done that already, by calling directly
Code:
sqlite_create_function($this->db->conn_id, 'regexp','regexp');
But I need that function to be declared on the beginning of my application, so it would be able for all subsequent queries. I tried using hooks, but when I put that line inside a hook function, I get this error, no matter what hook I'm using:
Quote:Undefined property: Creator::$db
Yes, of course I'm extending Controller.

Code:
<?php

function regexp($str, $regex) {
    return preg_match($regex, $str);
}  

class Creator extends Controller {
    function create_sqlite() {
        sqlite_create_function($this->db->conn_id, 'regexp','regexp');
    }
}

/* End of file creator.php */
/* Location: ./system/application/hooks/creator.php */

What's the correct way to process some logic at the beginning of the application?
Thanks!
#2

[eluser]Veehmot[/eluser]
My temporary solution is to add that line under
Code:
$this->_ci_initialize();
in Controller::Controller, but there has to be a way to process some logic at the start of the application without hacking the source. I thought that was the spirit of this application, to provide methods for developing without hacking the system, right?

Thanks,
#3

[eluser]Veehmot[/eluser]
Oh c'mon guys, this is an OpenSource project, the only support I have is this forum.
#4

[eluser]BrianDHall[/eluser]
Why not use the constructor of your controller, or if you prefer it be available for all controllers use the constructor of a MY_Controller?

If you prefer you can put your logic in a library/helper/plugin and autoload it (through the autoload.php file in your config directory, of course). That would work too.

Personally, I just use the constructor method. That's what constructors are for, after all.
#5

[eluser]Veehmot[/eluser]
I want it to be available for all controllers. What do you mean that I can use MY_Controller?
#6

[eluser]BrianDHall[/eluser]
OK, then a MY_Controller will be of use to you. It isn't the only way to do it, but it's very common way of handling it.

Basically you create a MY_Controller controller. The trick is then in all your other controllers instead of having:

Code:
class Catalog extends Controller {
    function Catalog()
    {
        parent::Controller();
        }
}

You change them to be:

Code:
class Catalog extends MY_Controller {
    function Catalog()
    {
        parent::MY_Controller();
        }
}

Now in your MY_Controller you can put in anything you want to be automatically available to all your controllers (or at least the ones you chose to use the MY_Controller.

So in your MY_Controller constructor you can put your call to sqlite_create_function() and define your regexp() function. Then in all of your controllers that you've made to extend MY_Controller you could call $this->regexp(), and your sqlite function will have already been executed for you and you will be ready to go!

This MY_Controller technique is typically used to create header/footer/etc variables, user authentication features, and all sorts of things like this.

Here's the basic manual entry on this: http://ellislab.com/codeigniter/user-gui...asses.html
#7

[eluser]Veehmot[/eluser]
Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB