Fast Path To B2C Commerce Developer Certification - Module 2 - Cartridges and Controllers
Fast Path To B2C Commerce Developer Certification - Module 2 - Cartridges and Controllers
Developer Certification
Module 2
Cartridges & Controllers
Module 2.1: Cartridges
• Objective
• By the end of this module you will be able to define what a cartridge is, understand SFRA
installation options, and understand the usage of the cartridge path
• This module will cover:
• SFRA base cartridge overview
• Cartridges SFRA + All Optional Features installation option
• Cartridge path meaning and usage
• What is this functionality and why does it matter for implementations?
• Cartridges are the way code is uploaded to a B2C instance
• Cartridge path affects how code gets executed in the site
• SFRA provides two starting points: base site or complex
Module 2.1: What is a cartridge?
The main container to deliver code
• Objective
• By the end of this module you will be able to define what a controller is,
understand the meaning of routes, exports and middleware functions, and create
a new controller that appends to a controller on the base
• This module will cover:
• Controller functionality
• Routes, exports, middleware functions
• Extending a controller using append and prepend
• What is this functionality and why does it matter for implementations?
• Controllers represent the URLs that are invoked in the storefront
• Custom controllers are the best way to extend the base SFRA controllers without
copying code
Module 2.2: What is a Controller?
● Routes represent the specific URL endpoints that the storefront uses:
In the Home.js controller, the Home-Show route is created with this code:
server.get('Show', consentTracking.consent, cache.applyDefaultCache, function (req, res, next) {
...
next();
}, pageMetaData.computedPageMetaData);
● The Home-Show route is invoked via this storefront URL (the homepage):
https://<your_instance>.net/on/demandware.store/Sites-RefArch-Site/en_US/ Home-Show
● Every controller is a CommonJS module, which is the convention that SFRA follows
● module.exports = server.exports(); makes all the routes available on the URL
● If you don’t export the routes, they are not available to the storefront
● middleware functions
execute as a chain
● each function receives req,
res and next as params
● consentTracking.consent presents the tracking consent modal that appears the first time you browse
the homepage
● cache.applyDefaultCache caches the homepage for a period of time:
○ This replaces the use of the <iscache> tag
○ There are other versions for different caching periods
Module 2.2: Usage of req, res, next in routes
Let’s look at the code inside a route
Module 2.2: Extending Controllers (and Models)
Modules can extend other modules in the cartridge path: not just controllers
Use of server.extend(module.superModule)
● The module.superModule global property provides
access to the most recent module on the cartridge path
with the same path/name as the current module
● A controller can extend or override another controller with
the same path/name without having to copy code from
one to the other: this results in reusability that
SiteGenesis controllers did not have
● There are 3 main methods used to extend or overwrite a
route:
○ append: extends functionality by executing AFTER
the superModule route
○ prepend: extends functionality by executing
BEFORE the superModule route
○ replace: overrides the superModule route
Module 2.2: Examples of Extending a Controller
Based on your current cartridge path, find some examples of extending
plugin_cartridge_merge:plugin_instorepickup:plugin_wishlists:plugin_giftregistry:lib_productlist:plugin_productcompare:plugin_site
map:plugin_applepay:plugin_datadownload:app_storefront_base
Example A: Example B:
● plugin_instorepickup/Cart.js extends ● plugin_wishlists/Account.js extends
app_storefront_base/Cart.js app_storefront_base/Account.js
● server.replace(‘AddProduct’...) executes ● server.prepend(‘Login’...) executes BEFORE
INSTEAD of the base AddProduct() the base Login() function
function ● server.append(‘Login’...) executes AFTER
the base Login() function
Module 2.2: Demo of Append and Prepend in Routes
DEMO
Time to Test your Knowledge!
Module 2.2: Controllers