Posted almost 3 years ago. Visible to the public.

Create A Bundle

Prerequisites

Official OroPlatform guide Archive 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 Archive 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 Archive .

Creating Bundle Manually

Follow this simple guide Archive .

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

Copy
<?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

Copy
bundles: - InventoryBundle\InventoryBundle

Clear the cache

Copy
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

Copy
<?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

Copy
bundles: - Stars\Bundle\LoggerEventBundle\StarsLoggerEventBundle

Error While Clearing Cache

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

Copy
[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:

Copy
[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 Archive , 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:

Copy
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:

Copy
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:

Copy
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!

Owner of this card:

Avatar
kiatng
Last edit:
almost 3 years ago
by kiatng
Attachments:
stopservice.png, z.png, z.png
Posted by kiatng to Oro
This website uses short-lived cookies to improve usability.
Accept or learn more