App Class

  • class Cake\Core\App
  • The App class is responsible for resource location and path management.

Finding Classes

  • static Cake\Core\App::classname($name, $type = '', $suffix = '')
  • This method is used to resolve classnames throughout CakePHP. It resolvesthe short form names CakePHP uses and returns the fully resolved classname:
  1. // Resolve a short classname with the namespace + suffix.
  2. App::classname('Auth', 'Controller/Component', 'Component');
  3. // Returns Cake\Controller\Component\AuthComponent
  4.  
  5. // Resolve a plugin name.
  6. App::classname('DebugKit.Toolbar', 'Controller/Component', 'Component');
  7. // Returns DebugKit\Controller\Component\ToolbarComponent
  8.  
  9. // Names with \ in them will be returned unaltered.
  10. App::classname('App\Cache\ComboCache');
  11. // Returns App\Cache\ComboCache

When resolving classes, the App namespace will be tried, and if theclass does not exist the Cake namespace will be attempted. If bothclassnames do not exist, false will be returned.

Finding Paths to Namespaces

  • static Cake\Core\App::path(string $package, string $plugin = null)
  • Used to get locations for paths based on conventions:
  1. // Get the path to Controller/ in your application
  2. App::path('Controller');

This can be done for all namespaces that are part of your application. Youcan also fetch paths for a plugin:

  1. // Returns the component paths in DebugKit
  2. App::path('Component', 'DebugKit');

App::path() will only return the default path, and will not be able toprovide any information about additional paths the autoloader is configuredfor.

  • static Cake\Core\App::core(string $package)
  • Used for finding the path to a package inside CakePHP:
  1. // Get the path to Cache engines.
  2. App::core('Cache/Engine');

Locating Plugins

  • static Cake\Core\Plugin::path(string $plugin)
  • Plugins can be located with Plugin. Using Plugin::path('DebugKit');for example, will give you the full path to the DebugKit plugin:
  1. $path = Plugin::path('DebugKit');

Locating Themes

Since themes are plugins, you can use the methods above to get the path toa theme.

Loading Vendor Files

Ideally vendor files should be autoloaded with Composer, if you have vendorfiles that cannot be autoloaded or installed with Composer you will need to userequire to load them.

If you cannot install a library with Composer, it is best to install each library ina directory following Composer’s convention of vendor/$author/$package.If you had a library called AcmeLib, you could install it intovendor/Acme/AcmeLib. Assuming it did not use PSR-0 compatible classnamesyou could autoload the classes within it using classmap in yourapplication’s composer.json:

  1. "autoload": {
  2. "psr-4": {
  3. "App\\": "src/",
  4. "App\\Test\\": "tests/"
  5. },
  6. "classmap": [
  7. "vendor/Acme/AcmeLib"
  8. ]
  9. }

If your vendor library does not use classes, and instead provides functions, youcan configure Composer to load these files at the beginning of each requestusing the files autoloading strategy:

  1. "autoload": {
  2. "psr-4": {
  3. "App\\": "src/",
  4. "App\\Test\\": "tests/"
  5. },
  6. "files": [
  7. "vendor/Acme/AcmeLib/functions.php"
  8. ]
  9. }

After configuring the vendor libraries you will need to regenerate yourapplication’s autoloader using:

  1. $ php composer.phar dump-autoload

If you happen to not be using Composer in your application, you will need tomanually load all vendor libraries yourself.