Welcome Guest, Not a member yet? Register   Sign In
Function which is run at the end
#1

[eluser]juan1904[/eluser]
Hi,

Is there any kind of function which is run after the constructor and the called function in a controller, but before the post_controller?
#2

[eluser]Dam1an[/eluser]
Not really sure what you're on about but you can't call a function 'function'

What exactly are you trying to acheive?
#3

[eluser]juan1904[/eluser]
Alright I'll explain a little further.

My solution to partial views is that I've created a post_controller where I load the views.

post_controller.php:

Code:
function post_controller()
{
    $CI =& get_instance();
    // Some data here between which is nessecary but not relevant for this question.

    $CI->load->view('partial/template', $data);
}

my main controller:

Code:
class Main extends Controller {

    function __construct()
    {
        parent::Controller();
    }

    function index()
    {
        $data['content'] = 'main';
        $this->load->vars($data);
    }
}

Because I am such a lazy person I don't want to write $this->load->vars($data) in every singel controller. So I thought there might be a function which is run after the function which is called by the view, so that I can write $this->load->vars($data) in that one instead.
#4

[eluser]Dam1an[/eluser]
Right, now I get you
I done a similar thing before
I put the data array in MY_Controller and had a method in there which loaded the vars (and the view partials)
All you need to do is make the data array and method public and use $this->data in your controllers and it should work
#5

[eluser]louis w[/eluser]
If I were you I would create a base controller which has this functionality built in and then extend it (instead of the CI Controller) for all of your controllers. This way you can reuse the code for each controller.

Also, check out the php built in method (if you have php5) __destruct. This gets called when a class is done executing.
#6

[eluser]juan1904[/eluser]
Dam1an:
If you load the vars in MY_Controller you need to call for that function in all your Controllers, that's exactly what I don't want to do. I want them to load automatically.

louis w:
__destruct seem like what I need, but it doesn't work for me. And yes, I have PHP5.

Do I need to write something like this in my controllers:
Code:
__destruct()
{
   parent::Controller();
}
#7

[eluser]Dam1an[/eluser]
Juan, you still need the hook into the method in MY_Controller to load the ars
The reason yuo're doing it there is so you can easily access the data array from your controllers

As for the destruct, you still need to declare it as a function
Code:
function __destruct() {
  // Anything else
  $this->load->vars($this->data);
  // Anything else
}
#8

[eluser]juan1904[/eluser]
Dam1en:

I wrote $this->load->vars($data); and not $this->load->vars($this->data);. Thanks to you I got it all figured out! Thank you alot!
#9

[eluser]juan1904[/eluser]
I'm a lier! It didn't work anyway. Dam1en, can you please give me a small code example of what I should do? I don't really understand right now.

Shall I copy the code form my post_controller to the destruct of the MY_Controller?
#10

[eluser]Dam1an[/eluser]
Can you post your code (MY_Controller, an example of a function that doesn;t work and the hooks) and I'll see if I can see where you're going wrong

I don't think I have any code examples of mine to show it in action, as it was an old project which I've since refactored (extracted it and added functionality into a seperate render library for added flexability)




Theme © iAndrew 2016 - Forum software by © MyBB