Laravel/Extend Aimeos

From Aimeos documentation

Laravel
Other languages:
English 100%


2016.x version

Create an extension

Aimeos and the Aimeos Laravel package are very powerful but there are numerous features that are only available through additional extensions. Often, your project also requires special features that makes it different from other web sites built with Aimeos. But changing or extending the Aimeos Laravel package itself is a bad idea because you will then lose the ability to update that extension.

To solve this, the Aimeos Laravel package allows you to integrate additional Aimeos extensions. Simply place your additional extensions in the ./ext/ directory of your Laravel application.

You can easily create a new Aimeos extension by using the extension generator

Use the Aimeos objects

If you need to instantiate the Aimeos controllers or managers directly from your Laravel application and call their methods, you have to supply a context object. This object is a dependency injection container that offers access to configuration settings, database connections, session and content cache as well as translation facilities. You can get an instance in Laravel via the "app" service container or the "App" facade:

  1. // in a controller action
  2. $context = $this->app->make('\Aimeos\Shop\Base\Context')->get();
  3. // in a command task
  4. $context = $this->getLaravel()->make('\Aimeos\Shop\Base\Context')->get(false);
  5. // anywhere else
  6. $context = App::make('\Aimeos\Shop\Base\Context')->get(false);

For a list of all possibilities, please have a look into the Laravel documentation.

The parameter of the get() method determines if a locale object with site, language and currency based on the request parameters will be automatically added to the context together with the translation facilities. Certainly, this is only possible in MVC controller actions where the required parameters are available as part of the request. Everywhere else, you need to retrieve this values from somewhere else, e.g. the configuration. Then, you can use the bootstrap() method of the locale manager to retrieve the locale item yourself:

  1. $manager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
  2. $item = $manager->bootstrap( '<sitecode>', '<languageid>', '<currencyid', true );
  3. $context->setLocale( $item );

The language and currency IDs are optional and the first matching locale item from the mshop_locale table in the database will be used. If there are no entries in the mshop_locale table because you want to manage the data in a custom administration interface, you can create an empty locale object yourself:

  1. $manager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
  2. $item = $manager->createItem();
  3. $item->setLanguageId( 'en' );
  4. $context->setLocale( $item );

It's a good idea to set at least the currently used language ID because then you are able to add the translation facilities for this language too and there won't be any exception if some code wants to translate a string:

  1. // use the appropriate way depending on your environment
  2. $i18n = $this->app->make('\Aimeos\Shop\Base\I18n')->get( array( 'en' ) );
  3. $context->setI18n( $i18n );

Alternatively, if you don't want any translation, you can add a "Null" object instead. It returns the singular or plural string untranslated and for this decision it needs the language ID to determine the right singular/plural rule:

  1. $i18n = array( 'en' => new \Aimeos\MW\Translation\None( 'en' ) );
  2. $context->setI18n( $i18n );

Afterwards, you are able to create every object from the Aimeos core and save, retrieve or delete the stored data. You should never use the "new" operator to create a new objects because the implementation variant depends on the configuration and decorators are added automatically. Instead, use the Aimeos\MShop\Factory class or any more specific factory to create new objects, e.g.

  1. $manager = \Aimeos\MShop\Factory::createManager( $context, 'product' );
  2. $search = $manager->createSearch();
  3. $search->setConditions( $search->compare( '==', 'product.code', 'test' ) );
  4.  
  5. foreach( $manager->searchItems( $search ) as $id => $item ) {
  6.     // print_r( $item );
  7. }