Welcome Guest, Not a member yet? Register   Sign In
Core not autoloading
#1

[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.
#2

[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.
#3

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

Paul
#4

[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.
#5

[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.
#6

[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());
   }

}
#7

[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
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB