MQL-Programmiererin Anja Vogel

Kurze Magento 2 Hilfe

von Magento Certified Developer

und Freelancer Anja Vogel

Nützliche Magento 2 Befehle

Tipp: Auf dem iPhone lassen sich die grauen Boxen mit dem Finger verschieben um den ganzen Inhalt sehen zu können.

1.) Im Controller bereits vorhanden

Request im Controller

/** @var \Magento\Framework\App\Request\Http $request */ $request = $this->getRequest(); $this->getRequest()->getParams(); $this->getRequest()->getPost(); $this->getRequest()->getQuery(); $this->getRequest()->getScheme(); $this->getRequest()->getRouteName() $this->getRequest()->getControllerName() $this->getRequest()->getActionName()

Response im Controller

/** @var \Magento\Framework\App\Response\Http\Interceptor $response */ $response = $this->getResponse(); $this->getResponse()->getHeaders(); $this->getResponse()->clearHeaders(); $this->getResponse()->setBody('Hello World');

Layout / Blocks

/** @var \Magento\Framework\App\View $this->_view */ $this->_view->getDefaultLayoutHandle() $this->_view->loadLayout(); /** @var \Magento\Framework\View\Layout\Interceptor $layout */ $layout = $this->_view->getLayout() $layout->getAllBlocks() $layout->getBlock('top.links')->toHtml(); $layout->getBlock('head.additional')->getTemplateFile(); $layout->getCacheTags(); $this->_view->renderLayout();

2.) Einfache Objekte per Dependency Injection

Per Dependency Injection werden Singleton-Objekte geholt.

Request im Observer

public function __construct(
        \Magento\Framework\App\RequestInterface $request
) {
    $this->request = $request;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $this->request->getParams();
}

Cookie

public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Magento\Framework\Session\SessionManagerInterface $sessionManager
) {
    parent::__construct($context);
    $this->cookieManager = $cookieManager;
    $this->sessionManager = $sessionManager;
}

public function execute()
{
    $this->getRequest()->getCookie('PHPSESSID');

    $this->cookieManager->getCookie('PHPSESSID');

    $this->sessionManager->getCookieDomain();
    $this->sessionManager->getCookieLifetime();
    $this->sessionManager->getCookiePath();
    $this->sessionManager->getName();  // = PHPSESSID

Session

public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession
) {
    parent::__construct($context);
    $this->customerSession = $customerSession;
}

public function execute()
{
    $this->customerSession->isLoggedIn();
    $this->customerSession->getCustomer();

Mögliche Sessions

\Magento\Customer\Model\Session \Magento\Checkout\Model\Session \Magento\Catalog\Model\Session \Magento\Backend\Model\Session \Magento\Newsletter\Model\Session \Magento\Persistent\Model\Session

Registry

public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry
) {
    parent::__construct($context);
    $this->coreRegistry = $coreRegistry;
}

public function execute()
{
    $this->coreRegistry->unregister('current_category');
    $this->coreRegistry->register('current_category', $category);
    $this->coreRegistry->registry('current_category');
    $this->coreRegistry->registry('current_product');

Config

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
    parent::__construct($context);
    $this->scopeConfig = $scopeConfig;
}

public function execute()
{
    $this->_scopeConfig->getValue('dev/translate_inline');
    $this->_scopeConfig->getValue('catalog/frontend');
    $this->_scopeConfig->getValue('catalog/productalert/allow_stock');
    $this->_scopeConfig->getValue('web/cookie');
    $this->_scopeConfig->getValue('web/default/cms_no_route');
    $this->_scopeConfig->getValue('customer/address_templates');
    $this->_scopeConfig->getValue('customer/startup/redirect_dashboard');
    $this->_scopeConfig->getValue('checkout');
    $this->_scopeConfig->getValue('checkout/options');
    $this->_scopeConfig->getValue('admin/captcha');

Verwendbare zweite Parameter

\Magento\Store\Model\ScopeInterface::SCOPE_STORE \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE

Store / Website

public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Store\Model\StoreManager $storeManager
) {
    parent::__construct($context);
    $this->storeManager = $storeManager;
}

public function execute()
{
    $store = $this->storeManager->getStore();
    $store->getName();
    $store->getId();
    $store->getCode();
    $store->getWebsiteId();
    $store->getCurrentCurrency();
    $store->getCurrentCurrencyCode();

    $website = $storeManager->getWebsite();

Locale

public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Locale\ResolverInterface $resolver,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
) {
    parent::__construct($context);
    $this->resolver = $resolver;
    $this->localeDate = $localeDate;
}

public function execute()
{
    $this->resolver->getLocale();
    $this->localeDate->getTimeFormat(); // = dd.MM.yy
    $this->localeDate->getDateFormat(); // = dd.MM.yy
    $this->localeDate->getDateFormatWithLongYear(); // = dd.MM.Y

Design

public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\DesignInterface $design,
) {
    parent::__construct($context);
    $this->design = $design;
}

public function execute()
{
    $this->design->getArea(); // = frontend
    $this->design->getDesignTheme()->getThemePath(); // = Magento/luma
    $this->design->getDesignTheme()->getData();

URLs

$store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); // z.B. http://www.anjavogel.de/ $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK); // z.B. https://www.anjavogel.de/ $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); // z.B. http://www.anjavogel.de/media/ $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC); // z.B. http://www.anjavogel.de/static/version1489477315/

Pfade

\Magento\Framework\App\Filesystem\DirectoryList::ROOT \Magento\Framework\App\Filesystem\DirectoryList::APP = app \Magento\Framework\App\Filesystem\DirectoryList::CONFIG = app/etc \Magento\Framework\App\Filesystem\DirectoryList::LIB_INTERNAL = lib/internal \Magento\Framework\App\Filesystem\DirectoryList::LIB_WEB = lib/web \Magento\Framework\App\Filesystem\DirectoryList::PUB = pub \Magento\Framework\App\Filesystem\DirectoryList::MEDIA = pub/media \Magento\Framework\App\Filesystem\DirectoryList::STATIC_VIEW = pub/static \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR = var \Magento\Framework\App\Filesystem\DirectoryList::TMP = var/tmp \Magento\Framework\App\Filesystem\DirectoryList::CACHE = var/cache \Magento\Framework\App\Filesystem\DirectoryList::LOG = var/log \Magento\Framework\App\Filesystem\DirectoryList::SESSION = var/session \Magento\Framework\App\Filesystem\DirectoryList::SETUP = setup/src \Magento\Framework\App\Filesystem\DirectoryList::DI = var/di \Magento\Framework\App\Filesystem\DirectoryList::GENERATION = var/generation \Magento\Framework\App\Filesystem\DirectoryList::UPLOAD = pub/media/upload \Magento\Framework\App\Filesystem\DirectoryList::COMPOSER_HOME = var/composer_home

3.) Normale Objekte per Factory

Factory-Klassen müssen nicht existieren. Sie werden dann von Magento’s object manager automatisch erstellt.

Models

public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Cms\Model\BlockFactory $blockFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Sales\Model\OrderFactory $orderFactory
) {
    parent::__construct($context);

    $this->blockFactory = $blockFactory;
    $this->productFactory = $productFactory;
    $this->categoryFactory = $categoryFactory;
    $this->customerFactory = $customerFactory;
    $this->orderFactory = $orderFactory;
}

public function execute()
{
    $cmsBlock = $this->blockFactory->create();
    $cmsBlockId = 7;
    $cmsBlock->load($cmsBlockId);

    $product = $this->productFactory->create();
    $productId = 10;
    $product->load($productId);

    $category = $this->categoryFactory->create();
    $categoryId = 2;
    $category->load($categoryId);

    $customer = $this->customerFactory->create();
    $customerId = 5;
    $customer->load($customerId);

    $order = $this->orderFactory->create();
    $orderId = 1;
    $order->load($orderId);

Collections

Die Methode getCollection() kann generell auf Models ausgeführt werden. Es ist nicht relevant, ob sie Daten enthalten oder nicht. Fortsetzung von Models:

    $this->blockFactory->create()->getCollection();
    $this->productFactory->create()->getCollection();
    $this->categoryFactory->create()->getCollection();
    $this->customerFactory->create()->getCollection();
    $this->orderFactory->create()->getCollection();

    $cmsBlock->getCollection();
    $product->getCollection()->count();
    $category->getCollection()->getFirstItem();
    $customer->getCollection()->getSize();
    $order->getCollection()->getSelect()->__toString();

4.) Codeschnipsel

Programmatisch erzeugter Block im Layout

public function execute()
{
    $this->_view->loadLayout();

    $block = $this->_view->getLayout()->addBlock(
        'Magento\Framework\View\Element\Text',
        'custom_block',
        'content');

    $block->setText("Hallo Welt!");

    $this->_view->renderLayout();
}

Magento 1

Hier geht es zurück zur kleinen Magento 1 Hilfe.