Quick Tip: PSR-0 Namespaces in Sugar 7

Post originally written by Jesse.

If you want to use namespaces following the PSR-0 standard for your custom code, it is now possible with the SugarAutoLoader in sugar7. Now you don't have to insert require statements all over the place.
require_once('include/utils/autoloader.php');

SugarAutoLoader::addNamespace('Fbsg\\', 'custom/include/Fbsg');

Sugar's autoloader will now map requests for the class Fbsg\Example\Test to the file "custom/include/Fbsg/Example/Test.php".

Post From Faye Business Systems Group Director of Development Matthew Strickland

Contact Jesse Heller

Parents
  • Comment originally made by Jelle Vink (@skymeyer).

    You can put the above snippet for example in custom/include/custom_utils.php which is automatically included through entryPoint (no need to include autoloader.php).

    Since Sugar 7.5 a better option is to register your namespaces directly in composer.json if you are familiar with composer. After adding your autoload definitions, you can run "composer update nothing" to apply the changes. SugarAutoLoader will take care of the rest for you - we support both PSR-0 and PSR-4.

    A knowledge base article will be available soon how to customize the composer configuration in SugarCRM. Perform a quick repair after running any composer command or manually remove cache/file_map.php and cache/class_map.php.

Comment
  • Comment originally made by Jelle Vink (@skymeyer).

    You can put the above snippet for example in custom/include/custom_utils.php which is automatically included through entryPoint (no need to include autoloader.php).

    Since Sugar 7.5 a better option is to register your namespaces directly in composer.json if you are familiar with composer. After adding your autoload definitions, you can run "composer update nothing" to apply the changes. SugarAutoLoader will take care of the rest for you - we support both PSR-0 and PSR-4.

    A knowledge base article will be available soon how to customize the composer configuration in SugarCRM. Perform a quick repair after running any composer command or manually remove cache/file_map.php and cache/class_map.php.

Children
No Data