CodeIgniter Forums
Core not autoloading - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Core not autoloading (/thread-19062.html)



Core not autoloading - El Forum - 05-26-2009

[eluser]CrazyMerlin[/eluser]
Hey people!

I have a file called loader.php which acts as an AJAX layer to the system. The file contains basic functions, no class.
In the registration form for a site I am building I check for the username existing when they enter one in the form.
I have a user controller which requests the user model to look for the username. However, the user controller throws an error when I instantiate it. If I explicitly require it before I instantiate it all is well, until it extends Controller which then throws the error. If I explicitly require Controller I would then need to do the same for all base classes used in Controller.

Is there a file with an autoload function that I need to require in my bootstrap, or will I need to create an __autoload() magic function?

Thanks,
Paul.


Core not autoloading - El Forum - 05-26-2009

[eluser]Pascal Kriete[/eluser]
Quote:Is there a file with an autoload function that I need to require in my bootstrap

Since CI is PHP 4 compatible, it doesn't provide an __autoload function. You are, of course, welcome to add your own though.

Quote:If I explicitly require Controller I would then need to do the same for all base classes used in Controller
Even if you require all other files, you will soon run into trouble. A lot of PHP scripts work backwards by including all dependencies needed for a file. CI on the other hand, works it's way up from index.php until it eventually instantiates the controller. If you're curious how it works take a look at system/codeigniter/codeigniter.php, where all major bootstrapping is done.

The easiest way to do ajax calls is to handle them directly in a regular controller. It depends on what loader.php does though.

Let me know if that clears things up at all.


Core not autoloading - El Forum - 05-26-2009

[eluser]CrazyMerlin[/eluser]
Thank you Pascal, that helps.
As I only work in PHP5 environments I will just create an autoload function.

Paul


Core not autoloading - El Forum - 05-26-2009

[eluser]Colin Williams[/eluser]
Seems odd to try using the framework piecemeal like this. I don't think the autoload function is going to clear everything up for you. As Pascal noted, there is more to the bootstrapping process than instantiating the controller.


Core not autoloading - El Forum - 05-27-2009

[eluser]CrazyMerlin[/eluser]
Well the autoload worked fine until of course I got to the loading of CI_DB which does not exist.
On top of that it became a little messy because CI does not follow classname/filename standards with classes being in files named not like the class and different case filenames.

What I need to do is simply initialize the system in my loader so that I can pass the data from forms using AJAX onto the controllers.

How would you best approach this?

Pascal said "The easiest way to do ajax calls is to handle them directly in a regular controller" but I don't see how.

Thanks.


Core not autoloading - El Forum - 05-27-2009

[eluser]Colin Williams[/eluser]
Point your Ajax calls at a /controller/function/id URI and then in the controller function, just return to the Ajax what is needed. CI doesn't force you to output a full HTML. You can output XML, JSON, pieces of HTML, whatever you need. It's still just a request/response cycle, it just happens in the framework. You have full control over the response.

A very (very, very) crude example...

Code:
class User extends Controller {
  
   function ajax_index()
   {
      $users = $this->db->get('users');
      echo json_encode($users->result());
   }

}



Core not autoloading - El Forum - 05-28-2009

[eluser]CrazyMerlin[/eluser]
Thanks Colin.

Currently I am using an xhr with POST data which is why I wasn't using the URI routing method.

I'l give that a go now.

Smile


Core not autoloading - El Forum - 05-28-2009

[eluser]Colin Williams[/eluser]
Well, you can always POST to a controller function. Good luck with it.