Tag Archives: Magento 2

Magento 2 Factory Object

Factory Object is used to instantiate an object

The Factory class name is the name of Model class and append with the Factory word. So for our example, we will have TopicFactory class. You must not create this class. Magento will create it for you. Whenever Magento’s object manager encounters a class name that ends in the word ‘Factory’, it will automatically generate the Factory class in the var/generation folder if the class does not already exist. You will see the factory class in

var/generation/<vendor_name>/<module_name>/Model/ClassFactory.php

To instantiate a model object we will use automatic constructor dependency injection to inject a factory object, then use factory object to instantiate the model object.

For example, we will call the model to get data in Block. We will create a Header block – PHPCodez\Layout\Block\Header.php with the following content.

<?php
 namespace PHPCodez\Layout\Block;
 class Header extends \Magento\Framework\View\Element\Template {
 
 protected $_headerContentFactory;
 
 public function __construct(\Magento\Framework\View\Element\Template\Context $context,
 \PHPCodez\Layout\Model\HeaderContentFactory $headerContentFactory) {
 $this->_headerContentFactory = $headerContentFactory;
 parent::__construct($context);
 }

public function getHeaderContent() {
 $headerContent = $this->_headerContentFactory->create();
 return $headerContent->process();
 }
 }

Here the block is calling the model function process().

XAMP Change Localhost To Domain

Follow the below steps to Change localhost to custom domain name in XAMP server

1) Open the file C:\Windows\System32\drivers\etc and edit “as Administrator” => hosts file.

Add the below line and save the file.

127.0.0.1 phpcodez.com

2) Edit httpd-vhosts.conf file (In my system E:\xampp\apache\conf\extra and edit httpd-vhosts.conf )

Add the below lines and save the file

<VirtualHost *:80>
 ServerName www.phpcodez.com
 ServerAlias phpcodez.com
 DocumentRoot e:/xampp/htdocs/phpcodez
</VirtualHost>

3) Restart XAMPP and url www.phpcodez.com will load content form e:/xampp/htdocs/phpcodez

Change XAMPP Server Port

Follow the below steps to update the defalt port 80 apache XAMP Apache server

1) Stop the XAMPP server, if it is running already.

2) Open the file httpd.conf(In my case its E:\xampp\apache\conf\httpd.conf)

Locate

Listen 80
ServerName localhost:80

And replace with the other port number(8012)

Listen 8012
ServerName localhost:8012

3) Open the file httpd-ssl(In my system its E:\xampp\apache\conf\extra\httpd-ssl.conf)

Locate the following lines

Listen 443
<VirtualHost _default_:443>
ServerName localhost:433

Replace them by with a other port number(8013)

Listen 8013
<VirtualHost _default_:8013>
ServerName localhost:8013

4) Restart the Apache server and http://localhost:8012/ should work.