Create A Bundle

Posted About 5 years ago. Visible to the public.

Prerequisites

Official OroPlatform guide Show archive.org snapshot on installation and setup.

What is a Bundle?

All OroPlatform-based applications have unique features that facilitate smooth development routine, like autoregistration of bundles and configuration files, for example.

However, these features assume that all application code is organized in bundles. For this reason, you have to create your own bundle for your custom code in order to perform customization tasks.

In short, we organized our custom codes into bundles. Think of it like modules.

Furthermore, Symfony doc Show archive.org snapshot says

A bundle is also a PHP namespace...A namespace becomes a bundle as soon as you add a bundle class to it.

What is a PHP Namespace?

Before PHP 5.3, every class is under the global scope, resulting in long class names with underscores as in Magento v1.x, and non-standard autoloading of classes. In PHP 5.3, namespace was introduced to provide local scope of classes. As with the underscore_long_class_name, the namespace is modeled after the directory structure. This enabled standardized autoloading, which is defined in PSR-4 Autoloading.

In short, namespace is the directory where a class resides. Think of it like an address for the classes so that autoloading knows how to look up any class.

More info Show archive.org snapshot .

Creating Bundle Manually

Follow this simple guide Show archive.org snapshot .

Create a definition file to init our bundle in src/{Our}Bundle/{Our}Bundle.php

<?php

namespace InventoryBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class InventoryBundle extends Bundle
{
}

Create a config file to enable our bundle in src/{Our}Bundle/Resources/config/oro/bundles.yml

bundles:
    - InventoryBundle\InventoryBundle

Clear the cache

kiat@win10 MINGW64 /d/Work/wamp64/www/oro/platform
$ php bin/console cache:clear

 // Clearing the cache for the dev environment with debug
 // true

 [OK] Cache for the "dev" environment (debug=true) was successfully cleared.

To speed up the above, launch Windows Services and stop the service Window Search:

Image

That's it.

Creating Bundle in a Company Directory

First file: src\Stars\Bundle\LoggerEventBundle\StarsLoggerEventBundle.php

<?php

namespace Stars\Bundle\LoggerEventBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class StarsLoggerEventBundle extends Bundle
{
}

Second file: src\Stars\Bundle\LoggerEventBundle\Resources\config\oro\bundles.yml

bundles:
    - Stars\Bundle\LoggerEventBundle\StarsLoggerEventBundle

Error While Clearing Cache

If we change the directory of our bundle, we may encounter the following error:

[kiat@reporting misoro]$ sudo -u nginx php ./bin/console cache:clear
PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "StarsLoggerEventBundle" from namespace "Stars\StarsLoggerEventBundle".
Did you forget a "use" statement for another namespace? in /usr/share/nginx/html/misoro/var/cache/dev/bundles.php:90
Stack trace:
#0 /usr/share/nginx/html/misoro/vendor/oro/platform/src/Oro/Bundle/DistributionBundle/OroKernel.php(70): require()
...

To proceed, we delete a cached file:

[kiat@reporting misoro]$ sudo rm  var/cache/dev/bundles.php
[kiat@reporting misoro]$ sudo -u nginx php ./bin/console cache:clear                  
 // Clearing the cache for the dev environment with debug
 // true

 [OK] Cache for the "dev" environment (debug=true) was successfully cleared.

Auto-Generate Bundle with CLI [Not Recommend]

In this OroCRM guide Show archive.org snapshot , it provides an example on how to create a bundle automatically. It says

It is important that you don’t need to update Kernel and routing, as OroPlatform provides its own way to do that, which will be described in the Enable bundle chapter and in following articles.

In Symfony 3.4, it enables Kernel and routing by force:

kiat@win10 MINGW64 /d/Work/wamp64/www/oro/platform-application (master)
$ php bin/console generate:bundle


  Welcome to the Symfony bundle generator!


Are you planning on sharing this bundle across multiple applications? [no]:

Your application code must be written in bundles. This command helps
you generate them easily.

Give your bundle a descriptive name, like BlogBundle.
Bundle name: MisBundle

Bundles are usually generated into the src/ directory. Unless you're
doing something custom, hit enter to keep this default!

Target Directory [src/]:

What format do you want to use for your generated configuration?

Configuration format (annotation, yml, xml, php) [annotation]:



  Bundle generation


> Generating a sample bundle skeleton into D:\Work\wamp64\www\oro\platform-appli
cation\src/../src/MisBundle
  created .\src/../src/MisBundle/
  created .\src/../src/MisBundle/MisBundle.php
  created .\src/../src/MisBundle/Controller/
  created .\src/../src/MisBundle/Controller/DefaultController.php
  created .\src/../tests/MisBundle/Controller/
  created .\src/../tests/MisBundle/Controller/DefaultControllerTest.php
  created .\src/../src/MisBundle/Resources/views/Default/
  created .\src/../src/MisBundle/Resources/views/Default/index.html.twig
  created .\src/../src/MisBundle/Resources/config/
  created .\src/../src/MisBundle/Resources/config/services.yml
> Checking that the bundle is autoloaded
OK
> Enabling the bundle inside D:\Work\wamp64\www\oro\platform-application\src\App
Kernel.php
  updated .\src\AppKernel.php
OK
> Importing the bundle's routes from the D:\Work\wamp64\www\oro\platform-applica
tion\src/config/routing.yml file
  created .\src/config/
  created .\src/config/routing.yml
OK
> Importing the bundle's services.yml from the D:\Work\wamp64\www\oro\platform-a
pplication\src/config/config.yml file
FAILED


  The command was not able to configure everything automatically.
  You'll need to make the following changes manually.


- Import the bundle's "services.yml" resource in the app's main configuration fi
le:

    - { resource: "@MisBundle/Resources/config/services.yml" }

Registering Bundle

Add config file, src\MisBundle\Resources\config\oro\bundles.yml:

bundles:
    - MisBundle\MisBundle

Go to homepage.

Error: Trying to register two bundles with the same name "MisBundle"

Image

To fix the error, go to src\AppKernel.php and comment:

public function registerBundles()
{
    $bundles = array(
    // bundles,
        //new MisBundle\MisBundle(), //auto-inserted in CLI generate:bundle
    );
    ...
}

And refresh page.

Follow these steps to verify that the bundle is registered:

Image

Remove Auto-Generated Routing Config

Lastly, remove the file src\config\routing.yml. This is because Oro Platform has its own routing.

That's it, we have a new bundle!

kiatng
Last edit
About 5 years ago
kiatng
Posted by kiatng to Oro (2019-02-21 04:39)