Jump to content

PSR-0 autoloader not loading classes


tom_burman

Recommended Posts

Im trying to implement the PSR-0 autoloader found here : Autoloader

 

I have simply copied and pasted the code as it is shown in the link so should work out of the box. Here is my file structure for my App:

 

Szur1.png

 

I believe i have set up the file structure correctly. App being the vendor, and then the folders below all being namespaces e.g. Frontend, Base, index will all be namespaces and subnamespaces.

The autoloader classes specifies this code in order the initiate the autoloader:

$classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
$classLoader->register();

And in my app i have used this:

$classLoader = new SplClassLoader('App', SITE_ROOT);
$classLoader->register();

The reason i have only put App as the namespace for the classLoader function is because i need to autoload all classes under the App folder.

 

Here is the initiation of my router class which i believe should be autoloaded so i don't have to include it:

$route = new router();

$route->setDefaultController(DEFAULT_CONTROLLER);
$route->setDefaultMethod(DEFAULT_METHOD);

$route->do_route();

$controller = $route->getController();
$method = $route->getMethod();
$arguments = $route->getArgs();

Is this correct? it doesnt seem to be loading any of the classes, not even the router class which is in the same directory as the autoloader?

 

Im getting pretty confused about what should be parsed to the SPLClassLoader function shown above for a namespace.

 

Any help in this area will be much appreciated

Thanks

 

Link to comment
Share on other sites

Thanks trq, that worked perfectly with my router class and i think i understand where i was going wrong. But i now have another issue, as you can see in my file directory i hav a Frontend->Base folders wand within it i have an index and profile folder.There will eventually be alot more folders for differnet parts of the website in here and each folder contains the controller and view for each module of the website.

 

The issue i know have is im not sure on how to autoload the controller classes because the destination folder e.g. frontend->base->index OR frontend->base->profile etc could be different each time.

 

I have called my router like this:

$route = new App\Core\router();

$route->setDefaultController(DEFAULT_CONTROLLER);
$route->setDefaultMethod(DEFAULT_METHOD);

$route->do_route();

$controller = $route->getController();
$method = $route->getMethod();
$arguments = $route->getArgs();

$controller = new App\Core\Frontend\Base\$controller . 'Controller.php';

As you can see at the bottom i have tried to make the correct controler but i cant reference a specific namespaced file because it will constantly be changing.

 

Any ideas?

 

Would psr-4 be better for this? or should i have another router within the Base folder or something?

 

thanks

Link to comment
Share on other sites

If {*} in the following line needs to be replaced dynamically, then that is the part your router would need to return.

 

App\Core\Frontend\{*}Controller
So, assuming you are trying to load the file App/Core/Frontend/Base/IndexController.php

 

And your code looks like:

 

$class = 'App\\Core\\Frontend\\Base\\' . $controller . 'Controller.php';
$controller = new $class;
Your router would need to return 'Foo\Index'.

 

Really though, why are you reinventing all this stuff? Its been done before.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.