Welcome Guest, Not a member yet? Register   Sign In
How I can set a function in all pages ?
#1

[eluser]lama[/eluser]
Hello everybody

I have a little problem...

I search in documentation but I found nothing...

I have a big project with CodeIgniter. I set a default layout where I include my views. Views are define with controller but I have to run a function on all page but I don't know how can I do it...

This function will show information to the user in the layout before including my view.

So I hope that I was clear... I'm blocked...

Thanks

Good night
#2

[eluser]adamck[/eluser]
I dont really understand.
You can run CI functions within a view, so a view can load another view etc...
Also you can pass data to a view, an array to a view or you can use the template parser.

You can put a function within a model, auto load the model and call that model function within each controller if you want...

It depends on the function your doing and what its going to do?

You need to provide more info!
#3

[eluser]PhilTem[/eluser]
search for: MY_Controller if the function needs to be run from the controller
or search for: helpers if the function needs can be run from the view
#4

[eluser]lama[/eluser]
The function needs to run before the controller and before the view.

I'm working on a little php game where the user makes x gold per hour. So I need to calcul the gold production before executing the view. And after in the view I show the gold.

Maybe I can include my code in __construct method of all controllers but I ask if there is a better way to do it.

Is it more clear ? :p
#5

[eluser]homebwoi[/eluser]
I am not sure that I clearly understood you, but if you want to do some actions before all controllers you can extend base controller. You should create file application/libraries/MY_Controller.php with this code:

Code:
class MY_Controller extends CI_Controller {

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

    // Do all your calculations here
  }

}

After that you should extend all your controllers from MY_Controller, no from CI_Controller. For example:
Code:
class Game extends MY_Controller {

}

Hope that's will help you.
#6

[eluser]PhilTem[/eluser]
That's just what I meant with telling you to search for MY_Controller: It will help you to run methods that need to be run on every page request made to any controller by injecting these methods into the __construct()-method.
#7

[eluser]lama[/eluser]
I tried with your method but it doesn't work when I put my controller in library folder, I have a critical error "class not found".

But when I put it in core folder I have no error but its seems that the controller do nothing.... or maybe session var doesn't work.

I tried the controller as a basic controller and it's working so I think that the problem isn't the controller code but the way I use it.
#8

[eluser]CroNiX[/eluser]
Yes, read the user guide on:
Creating Core Classes
Creating Libraries

Things have to go in specific places, depending what they are.

Extending CORE classes (prefixed with MY_) (things in /system/core) go in /application/core
Extending CORE libraries (prefixed with MY_) (things in /system/libraries) go in /application/libraries
#9

[eluser]PhilTem[/eluser]
You need to place MY_Controller into APPPATH . 'core/' since the CI class for it (CI_Controller) is in BASEPATH . 'core'/.

For the problem why it didn't work: If you create a file called MY_Controller.php in APPPATH . 'core/' and have these lines of code in it
Code:
<?php
class MY_Controller extends CI_Controller {
}

it will work. However, you will not see any difference in your other controllers as long as they continue extending CI_Controller but not MY_Controller. This part is the most crucial one: You need to have all controllers extend MY_Controller.

That's why it's usually best practice to create a MY_Controller immediately after unzipping the codeigniter downloadfile and make all your controllers extend that class - just because you can easily extend your code base without having to rewrite dozens of classes Wink
#10

[eluser]lama[/eluser]
It's running Big Grin

Thanks for help

My problem was that I wasn't in the constructor .... a newbie mistake, but I have to start somewhere ... :p

good night.




Theme © iAndrew 2016 - Forum software by © MyBB