Headless approach
With Magento
Sander Mangel
- Magento Developer
- Lead @ FitForMe
- Co-organiser MMNL,
Unconf, MageStackDay,
Meetups
- Avid community member
twitter.com/sandermangel
linkedin.com/in/sandermangel
github.com/sandermangel
Welcome to 2017
welcome to the future...
Read more
http://bit.ly/2qo6XBc
http://bit.ly/1Wku9c0
http://bit.ly/2rtVIas
http://bit.ly/2qrkQKP
http://bit.ly/2hyM6Dy
Source: https://laughingsquid.com/victorian-headless-portrait-photography/
What is headless architecture
Source: https://pantheon.io/blog/headless-websites-whats-big-deal-decoupled-architecture
What is headless architecture
"Headless architecture means an application offering APIs that abstract away business logic
for a multitude of clients to consistently and repeatedly execute the same tasks."
Putting this into practice
Clients
● Fulfillment dashboard
● Magento backend
● Magento frontend
● ...
● Magento will be a client & provide data
● Implement a basic data warehouse
● Which is fed by, and queried by various
services separated by domains
● Isolate data & business logic per domain
So we take a different approach
Leverage middleware for a stable environment
storage
source consumer server
Leverage middleware for a stable environment
● Normalize & combine data
● Offer stable APIs for clients
● Insulate from future changes
storage
source consumer server
Source: https://www.linkedin.com/pulse/business-analysisits-rocket-science-paul-lumsden
http://matrix.wikia.com/wiki/Matrix
Processing incoming data
{
"name": "ffm/headless-middleware",
"description": "Basic middleware for a remote API call",
"license": "MIT",
"authors": [],
"require": {
"php": ">=7.0",
"slim/slim": "^3.1",
"monolog/monolog": "^1.17",
"cache/filesystem-adapter": "^0.4.0",
"guzzlehttp/guzzle": "^6.2",
"cache/array-adapter": "^0.5.0",
"illuminate/database": "^5.4",
"yadakhov/insert-on-duplicate-key": "^1.1",
"symfony/console": "^3.3",
"league/oauth1-client": "^1.7"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"roave/security-advisories": "dev-master",
"friendsofphp/php-cs-fixer": "^2.1",
"phpmd/phpmd": "^2.6",
"phpro/grumphp": "^0.11.5",
"phpspec/phpspec": "^3.3",
"phpstan/phpstan": "^0.6.4",
"nikic/php-parser": "^3.0"
},
"autoload": {
"psr-4": {
"Ffm": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests": "tests/"
}
},
"scripts": {
"start": "php -S 0.0.0.0:8080 -t public public/index.php",
"test": "grumphp run"
}
}
Source: https://github.com/sandermangel/headless-middleware
● There is a solution for everything
already out there
● Use popular libraries, preferably
adhering to a PSR protocol
● Don't overcomplicate dependency
injection
● Think carefully about what you need and
spend time finding the best option
Build on standardised libraries
[...]
"require": {
"php": ">=7.0",
"slim/slim": "^3.1",
"monolog/monolog": "^1.17",
"cache/filesystem-adapter": "^0.4.0",
"guzzlehttp/guzzle": "^6.2",
"cache/array-adapter": "^0.5.0",
"illuminate/database": "^5.4",
"yadakhov/insert-on-duplicate-key": "^1.1",
"symfony/console": "^3.3",
"league/oauth1-client": "^1.7"
}
[...]
● slimphp - Routing, DI
● monolog - gives insight in what the
service does for monitoring and
debugging
● cache/… - stores certain API calls,
temporary data
● guzzle - offers a way to consume APIs
over http
● illuminate/database - Laravel ORM
(personal preference)
● Insert-on-duplicate.... - not a default
option in illuminate/database but a
must for me
● symfony/console - for cronjobs and
commandline tasks
● league/oauth1-client - offers built in
Magento 1 REST integration
Everything has a purpose
[...]
"require": {
"php": ">=7.0",
"slim/slim": "^3.1",
"monolog/monolog": "^1.17",
"cache/filesystem-adapter": "^0.4.0",
"guzzlehttp/guzzle": "^6.2",
"cache/array-adapter": "^0.5.0",
"illuminate/database": "^5.4",
"yadakhov/insert-on-duplicate-key": "^1.1",
"symfony/console": "^3.3",
"league/oauth1-client": "^1.7"
}
[...]
● SlimPHP project as basis
● One module in src some basic setup
files needed by SlimPHP
● Keep it simple and self-contained
Structure
Storage
● SQL based / relational
● Offer good performance up to 1TB data
● Easy to integrate
● Be the best solution for the upcoming
year
Choice of database
Exposing data
● Read only
● Offer sorting, filtering, and a host of
other query options
● Be very fast (d'oh)
● Be standardised & easily scalable
Making data available for clients
Source: https://www.archaeological.org/fieldwork/afob/10374#
Search
Leveraging the built in REST api of
Elastic we had a ready to go layer.
We decided to leave that 'as is' and
deal with changes in data structure or
due to a version upgrade as they come
Each search result has, next to the
record data, a reference to a UID from
the SQL database to retrieve the full
data set.
Datasets
● As stated the API server is read
only
● For each dataset (a table in a
database) there is one endpoint
● Simple filter methods on one or
more fields
● Each returned dataset has
reference UIDs to other records
that are tied to this item.
So for customers this can be
orders or addresses
● Multiple API calls over large or
joined datasets
Summary
● By offloading tasks from Magento we
keep it fast and lightweight
● Storing data in a central place and
making it available via REST apis makes
for easy integration
● Headless can start by adding some of
those APIs to the existing back- or
frontend
● Using lightweight php frameworks with
limited features makes for easy to
develop and fast applications
Summary
Source: https://www.archaeological.org/fieldwork/afob/10374#
https://joind.in/talk/8365d
Code:
https://github.com/sandermangel/headl
ess-middleware
twitter.com/sandermangel
github.com/sandermangel

Headless approach for offloading heavy tasks in Magento

  • 1.
  • 2.
    Sander Mangel - MagentoDeveloper - Lead @ FitForMe - Co-organiser MMNL, Unconf, MageStackDay, Meetups - Avid community member twitter.com/sandermangel linkedin.com/in/sandermangel github.com/sandermangel
  • 3.
    Welcome to 2017 welcometo the future...
  • 4.
  • 5.
    What is headlessarchitecture Source: https://pantheon.io/blog/headless-websites-whats-big-deal-decoupled-architecture
  • 6.
    What is headlessarchitecture "Headless architecture means an application offering APIs that abstract away business logic for a multitude of clients to consistently and repeatedly execute the same tasks."
  • 7.
  • 9.
    Clients ● Fulfillment dashboard ●Magento backend ● Magento frontend ● ...
  • 10.
    ● Magento willbe a client & provide data ● Implement a basic data warehouse ● Which is fed by, and queried by various services separated by domains ● Isolate data & business logic per domain So we take a different approach
  • 12.
    Leverage middleware fora stable environment storage source consumer server
  • 13.
    Leverage middleware fora stable environment ● Normalize & combine data ● Offer stable APIs for clients ● Insulate from future changes storage source consumer server
  • 14.
  • 15.
  • 16.
  • 17.
    { "name": "ffm/headless-middleware", "description": "Basicmiddleware for a remote API call", "license": "MIT", "authors": [], "require": { "php": ">=7.0", "slim/slim": "^3.1", "monolog/monolog": "^1.17", "cache/filesystem-adapter": "^0.4.0", "guzzlehttp/guzzle": "^6.2", "cache/array-adapter": "^0.5.0", "illuminate/database": "^5.4", "yadakhov/insert-on-duplicate-key": "^1.1", "symfony/console": "^3.3", "league/oauth1-client": "^1.7" }, "require-dev": { "phpunit/phpunit": "^6.0", "roave/security-advisories": "dev-master", "friendsofphp/php-cs-fixer": "^2.1", "phpmd/phpmd": "^2.6", "phpro/grumphp": "^0.11.5", "phpspec/phpspec": "^3.3", "phpstan/phpstan": "^0.6.4", "nikic/php-parser": "^3.0" }, "autoload": { "psr-4": { "Ffm": "src/" } }, "autoload-dev": { "psr-4": { "Tests": "tests/" } }, "scripts": { "start": "php -S 0.0.0.0:8080 -t public public/index.php", "test": "grumphp run" } } Source: https://github.com/sandermangel/headless-middleware
  • 18.
    ● There isa solution for everything already out there ● Use popular libraries, preferably adhering to a PSR protocol ● Don't overcomplicate dependency injection ● Think carefully about what you need and spend time finding the best option Build on standardised libraries [...] "require": { "php": ">=7.0", "slim/slim": "^3.1", "monolog/monolog": "^1.17", "cache/filesystem-adapter": "^0.4.0", "guzzlehttp/guzzle": "^6.2", "cache/array-adapter": "^0.5.0", "illuminate/database": "^5.4", "yadakhov/insert-on-duplicate-key": "^1.1", "symfony/console": "^3.3", "league/oauth1-client": "^1.7" } [...]
  • 19.
    ● slimphp -Routing, DI ● monolog - gives insight in what the service does for monitoring and debugging ● cache/… - stores certain API calls, temporary data ● guzzle - offers a way to consume APIs over http ● illuminate/database - Laravel ORM (personal preference) ● Insert-on-duplicate.... - not a default option in illuminate/database but a must for me ● symfony/console - for cronjobs and commandline tasks ● league/oauth1-client - offers built in Magento 1 REST integration Everything has a purpose [...] "require": { "php": ">=7.0", "slim/slim": "^3.1", "monolog/monolog": "^1.17", "cache/filesystem-adapter": "^0.4.0", "guzzlehttp/guzzle": "^6.2", "cache/array-adapter": "^0.5.0", "illuminate/database": "^5.4", "yadakhov/insert-on-duplicate-key": "^1.1", "symfony/console": "^3.3", "league/oauth1-client": "^1.7" } [...]
  • 20.
    ● SlimPHP projectas basis ● One module in src some basic setup files needed by SlimPHP ● Keep it simple and self-contained Structure
  • 21.
  • 23.
    ● SQL based/ relational ● Offer good performance up to 1TB data ● Easy to integrate ● Be the best solution for the upcoming year Choice of database
  • 25.
  • 26.
    ● Read only ●Offer sorting, filtering, and a host of other query options ● Be very fast (d'oh) ● Be standardised & easily scalable Making data available for clients Source: https://www.archaeological.org/fieldwork/afob/10374#
  • 27.
    Search Leveraging the builtin REST api of Elastic we had a ready to go layer. We decided to leave that 'as is' and deal with changes in data structure or due to a version upgrade as they come Each search result has, next to the record data, a reference to a UID from the SQL database to retrieve the full data set.
  • 28.
    Datasets ● As statedthe API server is read only ● For each dataset (a table in a database) there is one endpoint ● Simple filter methods on one or more fields ● Each returned dataset has reference UIDs to other records that are tied to this item. So for customers this can be orders or addresses ● Multiple API calls over large or joined datasets
  • 29.
  • 30.
    ● By offloadingtasks from Magento we keep it fast and lightweight ● Storing data in a central place and making it available via REST apis makes for easy integration ● Headless can start by adding some of those APIs to the existing back- or frontend ● Using lightweight php frameworks with limited features makes for easy to develop and fast applications Summary Source: https://www.archaeological.org/fieldwork/afob/10374#
  • 31.