EllisLab text mark
Advanced Search
     
How I can set a function in all pages ?
Posted: 14 November 2012 04:58 PM   [ Ignore ]
Joined: 2012-11-14
6 posts

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

 
Posted: 14 November 2012 06:23 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2012-07-27
27 posts

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!

 
Posted: 14 November 2012 07:00 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

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

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 15 November 2012 11:59 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-11-14
6 posts

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

 
Posted: 15 November 2012 12:59 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-07-24
3 posts

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:

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:

class Game extends MY_Controller {

Hope that’s will help you.

 
Posted: 15 November 2012 01:38 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

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.

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 15 November 2012 03:27 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-11-14
6 posts

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.

 
Posted: 15 November 2012 03:57 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3801 posts

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

 Signature 
 
Posted: 15 November 2012 04:00 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

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

<?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

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 15 November 2012 04:52 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2012-11-14
6 posts

It’s running :D

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.