0

I'm new to CodeIgniter, so this may sound a bit stupid.

But here's what I want :

  • Have my own classes, with functions, etc
  • Be able to use them either in a view or a controller

(I've thought of simply require_onceing the necessary files, or perhaps creating a CI Library, but I'm still not sure)

What's the most CI-friendly and efficient way you would suggest? Ideas?

1 Answer 1

3

Use the libraries:

$this->load->library('my_class');
$this->my_class->some_function();

EDIT:

It is not recommended, but if you must use the library in the view, use it like this:

$data['my_class'] =& $this->my_class;
$this->load->view('my_view', $data);  
Sign up to request clarification or add additional context in comments.

5 Comments

Well, that's what I was thinking too... Thanks (again)!
One question though : Let's say I set up a library, and autoload it. How could I use it from my view??
You shouldn't do it from inside the view. Do as much work as possible in the model/controller. Read here: stackoverflow.com/questions/2622745/…
Hmmm... I get your point, but what I want to do is basically group a few repetitive html-producing functions (like "echo 3 input fields"), which is too view-oriented (wouldn't that... "violate" the MVC logic, if I stuff that in a controller?). An idea : would creating a "Helper" sound more sensible?
A helper function sounds reasonable. This code is not class specific, thus there is no need to send an entire class object to the view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.