tom_burman Posted May 11, 2014 Share Posted May 11, 2014 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: 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 Quote Link to comment https://forums.phpfreaks.com/topic/288420-psr-0-autoloader-not-loading-classes/ Share on other sites More sharing options...
trq Posted May 11, 2014 Share Posted May 11, 2014 Your classes need to be namespaced properly, meaning you would instantiate your router using: $router = new App\Core\Router(); Quote Link to comment https://forums.phpfreaks.com/topic/288420-psr-0-autoloader-not-loading-classes/#findComment-1479125 Share on other sites More sharing options...
tom_burman Posted May 12, 2014 Author Share Posted May 12, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/288420-psr-0-autoloader-not-loading-classes/#findComment-1479163 Share on other sites More sharing options...
trq Posted May 12, 2014 Share Posted May 12, 2014 Your router is going to need to return more useful information about where you controller lives than just the short class name. Quote Link to comment https://forums.phpfreaks.com/topic/288420-psr-0-autoloader-not-loading-classes/#findComment-1479165 Share on other sites More sharing options...
tom_burman Posted May 12, 2014 Author Share Posted May 12, 2014 Could you give me an example please? i'm pretty lost at the moment! first time using namespaces and PSR. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/288420-psr-0-autoloader-not-loading-classes/#findComment-1479171 Share on other sites More sharing options...
trq Posted May 12, 2014 Share Posted May 12, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288420-psr-0-autoloader-not-loading-classes/#findComment-1479175 Share on other sites More sharing options...
tom_burman Posted May 12, 2014 Author Share Posted May 12, 2014 thankyou very much, ill give it a go! Why am i reinventing another framework? For learning mainly my friend, just trying to get better using PHP etc Quote Link to comment https://forums.phpfreaks.com/topic/288420-psr-0-autoloader-not-loading-classes/#findComment-1479177 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.