|
| 1 | +RobotLoader: comfortable autoloading |
| 2 | +==================================== |
| 3 | + |
| 4 | +[](https://packagist.org/packages/nette/robot-loader) |
| 5 | +[](https://travis-ci.org/nette/robot-loader) |
| 6 | + |
| 7 | +RobotLoader is a tool that gives you comfort of automated class loading for your entire application including third-party libraries. |
| 8 | + |
| 9 | +- get rid of all `require` |
| 10 | +- only necessary scripts are loaded |
| 11 | +- requires no strict file naming conventions |
| 12 | +- allows more classes in single file |
| 13 | + |
| 14 | +So we can forget about those famous code blocks: |
| 15 | + |
| 16 | +```php |
| 17 | +require_once 'Zend/Pdf/Page.php'; |
| 18 | +require_once 'Zend/Pdf/Style.php'; |
| 19 | +require_once 'Zend/Pdf/Color/GrayScale.php'; |
| 20 | +require_once 'Zend/Pdf/Color/Cmyk.php'; |
| 21 | +... |
| 22 | +``` |
| 23 | + |
| 24 | + |
| 25 | +Like the Google robot crawls and indexes websites, RobotLoader crawls all PHP scripts and records what classes and interfaces were found in them. |
| 26 | +These records are then saved in cache and used during all subsequent requests. You just need to specifiy what directories to index and where to save the cache: |
| 27 | + |
| 28 | +```php |
| 29 | +$loader = new Nette\Loaders\RobotLoader; |
| 30 | +// Add directories for RobotLoader to index |
| 31 | +$loader->addDirectory('app'); |
| 32 | +$loader->addDirectory('libs'); |
| 33 | +// And set caching to the 'temp' directory on the disc |
| 34 | +$loader->setCacheStorage(new Nette\Caching\Storages\FileStorage('temp')); |
| 35 | +$loader->register(); // Run the RobotLoader |
| 36 | +``` |
| 37 | + |
| 38 | +And that's all. From now on, you don't need to use `require`. Great, isn't it? |
| 39 | + |
| 40 | +When RobotLoader encounters duplicate class name during indexing, it throws an exception and informs you about it. |
| 41 | + |
| 42 | +The variable `$loader->autoBuild` determines whether RobotLoader should reindex the scripts if asked for nonexistent class. |
| 43 | +This feature is disabled by default on production server. |
| 44 | + |
| 45 | +If you want RobotLoader to skip some directory, create a file there called `netterobots.txt`: |
| 46 | + |
| 47 | +``` |
| 48 | +Disallow: /Zend |
| 49 | +``` |
| 50 | + |
| 51 | +From this point on, the Zend directory will not be indexed. |
| 52 | + |
| 53 | +RobotLoader is extremely comfortable and addictive! |
0 commit comments