Welcome Guest, Not a member yet? Register   Sign In
Accessing classes
#1

[eluser]garrettheel[/eluser]
Sorry.. I'm completely new to CodeIgniter. Just wondering, I have two controllers which extend the "controller" class. How can I access a function in controller one from controller two? I'm getting a class not found error.
#2

[eluser]Jamie Rumbelow[/eluser]
By default you can't. CodeIgniter instantiates a controller class only if it is needed i.e if it's in the url.

If needed you can load the file and do it yourself, but this is not recommended.

Code:
include( APPPATH . "controllers/whatever.php" );

$Class = new Whatever();
#3

[eluser]dpgtfc[/eluser]
Best be is to use a library or helper for functions used by multiple controllers.

Or you can look into the Modular extensions library.
#4

[eluser]Ty Bex[/eluser]
I think if you are new Codeigniter then reading this from wrox. It does an excellent job of explaining CodeIgniter and simple real world examples. I am in no way Afflicated with wrox but I have used this book and may others of theirs.

CodeIgniter Professional Book from WROX

Professional CodeIgniter
Thomas Myer
ISBN: 978-0-470-28245-8
Paperback
336 pages
July 2008
#5

[eluser]zippy[/eluser]
I'm with dpgtfc on this. Shared functionality ought to be extracted to a helper function (simple case) or a library class (complex case).

What's often overlooked is the fact that a controller can hand-off (i.e., delegate) to a library class by passing its own instance ($this) to the library's constructor, or to some method of the library class.

With the controller instance in hand, the library (or its method) has unfettered access to all of the properties and methods of the controller; it is, after all, acting as delegate to the controller.

This is very powerful. It can help remove any amount of redundant code from your controller(s).
#6

[eluser]garrettheel[/eluser]
Thanks for all the replies.

So I get that I'll need to use a custom helper/library to achieve what I want. So basically what I'm trying to make is a page handler that is called at the end of a function when I want to display a page instead of $this->load->view('blah');. The page handler instead would take the page I want to load and data as arguments and put it between the global header and footer. Would a library or helper be appropriate in this case?
#7

[eluser]garrettheel[/eluser]
[quote author="Ty Bex" date="1228607352"]I think if you are new Codeigniter then reading this from wrox. It does an excellent job of explaining CodeIgniter and simple real world examples. I am in no way Afflicated with wrox but I have used this book and may others of theirs.

CodeIgniter Professional Book from WROX

Professional CodeIgniter
Thomas Myer
ISBN: 978-0-470-28245-8
Paperback
336 pages
July 2008[/quote]
Thanks for the tip, I'll try and find this book.
#8

[eluser]zippy[/eluser]
@garrettheel

Man you got me flummoxed.

I have read your original question and your follow-up a few dozen times. I still can't imagine what context your issues occupy, and you're not helping yourself here.

Put your issue/problem into a context others can relate to, and you'll surely get a sensible response.

FFS.
#9

[eluser]garrettheel[/eluser]
No need to get irritated, just tell me what you don't understand. I tried to explain it as best I could.

I just want to know whether a library or module would be better for a class I would have which would take a view name as an input and output the header, footer and the view inputted. It's so I don't have to keep calling the header and footer all the time.
#10

[eluser]dpgtfc[/eluser]
[quote author="garrettheel" date="1228627772"]No need to get irritated, just tell me what you don't understand. I tried to explain it as best I could.

I just want to know whether a library or module would be better for a class I would have which would take a view name as an input and output the header, footer and the view inputted. It's so I don't have to keep calling the header and footer all the time.[/quote]

Why don't you use the template library built into CI?




Theme © iAndrew 2016 - Forum software by © MyBB