1

Where should I keep my autoload class and router class?

As I understand a router class is not part of the design patterns so I should store it in a folder, say facade, then where should I keep it?

also, what about the autoload class, is it not part of any design patterns? where should I keep it then?

where do all php frameworks keep these two classes? I try to look for them in Laravel, CakePHP, Zend Framework, etc, to get some ideas where they should be kept, but I can't find them in these frameworks. Any ideas?

router class,

class Router 
{
...
}

autoload class,

class Autoloader
{
...
// spl_autoload_register(array($this, 'load'));
}
oasis
  • 197
  • 2
  • 13

2 Answers2

1

Cake Custom Router Class: http://book.cakephp.org/2.0/en/development/routing.html#custom-route-classes

I have Autoload in app/Vendor/autoload.php, it is effectively a link to app/Vendor/Composer/autoload_real.php created by Composer. Info here: http://book.cakephp.org/2.0/en/installation/advanced-installation.html#installing-cakephp-with-composer

If you are writing a totally custom autoload I think you could also sensibly create /app/Autoload/ and add it here. I do something similar for events - creating /app/Event/.. - it follows the conventions of Cake's file structure and works well. I'm not aware of any specified method within the Cake docs for creating a custom autoload class.

More info on loading classes here: http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#loading-classes

Simon Mason
  • 561
  • 2
  • 10
1

Ok lets take the modern approach. None of the frameworks now want a autoloader. Simply follow the psr-4 (or psr-0 if you want) standard for autoloading php classes. Always use composer to autoload your files and dependencies.

Say you need to autoload all your classes for a custom project. All of your classes are in App directory. How do you do it? In your composer.json file

"autoload": { "psr-4": { "MyAppName\\" : "App" } }

Now if you follow psr-4 and require "vendor/autoload.php" in any file and all of your classes will be autoloaded whenever required.

Hope I could answer your questions. You can use use classmap of composer if you want but not suggested.

FlatLander
  • 1,717
  • 15
  • 21
  • thanks for the answer. Can I ask - why `None of the frameworks now want a autoloader.`? I know how to use Composer to generate the autoloader, but I prefer to be independent on other vendors, such as Composer. I don't see the benefits of using Composer for a autoloader. So what is it good about it? – oasis Mar 10 '15 at 10:30
  • 1
    @oasis Composer has become the defacto standard PHP dependency manager, pretty much every even rudimentarily serious project you can find offers composer support. It's a drop in solution that takes away all the pain of maintaining dependencies and the various autoloading mechanisms that these dependencies and also your own project code may require. What's good about that is pretty much self-explantory if you ask me. – ndm Mar 10 '15 at 11:57