This tutorial is aimed at plentymarkets users who are getting started with plugin development.
You will only need basic knowledge of creating and editing files in an integrated development environment (IDE) as well as using plentymarkets.
In six simple steps, we will walk you through creating your first plugin that displays Hello World! in your browser when adding /hello
to your domain.
We recommend using an IDE which supports the PHP language and Twig syntax. To get started, install Atom. It can be extended to support Twig syntax.
*) All required Atom packages can be found in the embedded package control pane of the Atom IDE. To access this pane, open the Atom » Preferences menu and click on Install.
Sign up for a hosting provider for your plugin, e.g. GitHub. You will need the login details when cloning your plugin to your plentymarkets inbox in step 5.
HelloWorld
.
Install a desktop client for GitHub in order to clone the repository to your local computer.
The components of your plugin are the foundation for the communication between the plugin and plentymarkets. A ServiceProvider is needed for registering the plugin in plentymarkets. A RouteServiceProvider creates and registers the output route. And a ContentController renders and displays your content in your template.
All plugins need to be organised in a specific structure. We only need the following folders for our plugin. The resources folder can be organised in sub-folders. Here, we save our template. The src folder can also be organised in sub-folders and contains the plugin source code.
HelloWorld/ ├── resources/ │ └── views/ │ └── content/ │ └── // twig templates (can be organised in sub-folders) │ ├── src/ │ ├── Providers/ │ └── Controllers/ │ └── plugin.json // plugin information
All information about your plugin is defined in a plugin.json
file. This file defines the service
provider of your plugin, which will be called by plentymarkets to register and run your plugin.
plugin.json
.
HelloWorld/plugin.json
{ "name" :"HelloWorld", "description" :"My first plugin", "namespace" :"HelloWorld", "author" :"Your name", "type" :"template", "serviceProvider" :null }
Important notice: Do not use special characters or spaces for the name
attribute, otherwise your plugin will not be read properly. We recommend using camel case notation, for example: MyFirstPlugin
HelloWorldServiceProvider.php
.
HelloWorld/src/Providers/HelloWorldServiceProvider.php
<?php namespace HelloWorld\Providers; use Plenty\Plugin\ServiceProvider; class HelloWorldServiceProvider extends ServiceProvider { /** * Register the service provider. */ public function register() { } }
HelloWorldRouteServiceProvider.php
.
HelloWorld/src/Providers/HelloWorldRouteServiceProvider.php
<?php namespace HelloWorld\Providers; use Plenty\Plugin\RouteServiceProvider; use Plenty\Plugin\Routing\Router; class HelloWorldRouteServiceProvider extends RouteServiceProvider { public function map(Router $router) { } }
Now we have to register the RouteServiceProvider in the ServiceProvider.
To do so, add a line in the code of HelloWorldServiceProvider.php
.
HelloWorld/src/Providers/HelloWorldServiceProvider.php
... public function register() { $this->getApplication()->register(HelloWorldRouteServiceProvider::class); }
ContentController.php
.
HelloWorld/src/Controllers/ContentController.php
<?php namespace HelloWorld\Controllers; use Plenty\Plugin\Controller; use Plenty\Plugin\Templates\Twig; class ContentController extends Controller { public function sayHello(Twig $twig):string { return $twig->render('HelloWorld::TEMPLATE'); } }
sayHello
function that renders a twig template. The render
method specifies the template location: 'PLUGINNAME::TEMPLATE'
. Since templates are always saved in the resources/views folder in your plugin, we only have to specify part of the template path. Note that PLUGINNAME
is the name of the plugin folder. The name of the plugin folder and the plugin namespace may differ.
Now we have to map the route for the ContentController in the RouteServiceProvider. To do so, add a line in the code.
HelloWorld/src/Providers/HelloWorldRouteServiceProvider.php
... public function map(Router $router) { $router->get('hello','HelloWorld\Controllers\ContentController@sayHello'); }
get
method to pass two parameters. The first parameter 'hello'
defines the route. The second parameter consists of the Fully-Qualified Class Name and the @
controller method that is called when the route is called.
hello.twig
.
<h1>Hello World!</h1>
.
Here, we have to specify the template path in the ContentController. Simply replace TEMPLATE
with the correct path.
HelloWorld/src/Controller/ContentController.php
... public function sayHello(Twig $twig):string { return $twig->render('HelloWorld::content.hello'); }
plugin.json
file.
"HelloWorld\\Providers\\HelloWorldServiceProvider"
.
Next, the files in your local HelloWorld folder must be pushed into your Git repository.
In this step, you have to clone the Git repository into your plentymarkets inbox. You will need the remote URL from GitHub as well as your login details.
Deploy the plugin to integrate it with plentymarkets.
Finally, open a new browser tab and type in your domain adding /hello
at the end. And there it is, your first plugin.
HelloWorld
folder, the changes must be pushed to GitHub. You also have to update the plugin in your plentymarkets inbox by pulling the changes from GitHub. Finally, you have to deploy the plugin again to display the changes in your browser.