5. Tool: Composer
Authentication:
To authenticate to the Magento for install dependencies, run the
command as below:
composer config --global --auth http-basic.repo.magento.com
MAGENTO_COMPOSER_USER MAGENTO_COMPOSER_PASS
USER and PASS are public and private public key in your Magento
Account.
Usage:
composer install
composer update
composer require <vendor>/<packages>
Notes:
Do not use composer with sudo privileges.
7. Tool: n98 magerun v2
New version of n98 magerun for Magento 2.
Extends the console components.
Using Symfony Console.
Available at: https://github.com/netz98/n98-magerun2
Useful Command:
sys:info
sys:setup:change-version
sys:setup:downgrade-versions
db:dump
Tricks:
Using Symfony shortcut syntax, example just type `sy:i`
instead of full options as `sys:info`
8. Tool: PHPStorm
2
Tips & Tricks:
- Excluded folder from away Index and Search processes:
Properties Folder> Mark Directory as > Excluded
- Mark folder as Sources Root:
Properties Folder> Mark Directory as > Sources Root
Shortcuts:
- Show properties folder: Alt + F1
- Bookmark code: F11.
- Show all bookmark: Alt + F2
- Rename file: Shift + F6
- Show all breakpoint: Ctrl + Shift + F8
- Generate code snippet: Alt + Insert
13. LAMP & LEMP
2
Prerequisite:
- Apache 2.4 or Nginx
- PHP 7.0 with FPM
- Mysql 5.6
- PHP extensions:
php7.0-mysql php7.0-mcrypt php7.0-curl php7.0-gd libcurl3 php7.0-intl
php7.0-xsl php7.0-zip php7.0-mbstring php7.0-dom php7.0-simplexml
php7.0-xml
Note:
- If you using Apache, do not delete .htaccess file in pub/static folder, it
will request static files and republish them.
- Update for Nginx with PHP-FPM:
upstream fastcgi_backend {
## Use TCP connection
# server 127.0.0.1:9000;
## Or socket
server unix:/run/php/php7.0-fpm.sock;
}
14. Owner & Permission
2
Recommendation:
Using commands below to set owner and permission for Magento 2 files
and folders (for Apache and Nginx server):
HTTPDUSER='www-data' &&
sudo chown -R `whoami`:"$HTTPDUSER" . &&
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX var
pub/static pub/media app/etc &&
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX var
pub/static pub/media app/etc &&
find . -type d -exec chmod 775 {} ; && find . -type f -exec chmod 664 {} ;
&&
chmod u+x bin/magento
15. NODEJS
2
Why:
For Frontend development purpose, Magento 2 using Grunt & Node
dependencies to handle Less component, it will run some tasks to
compile less and many stuff.
Install NODEJS and NPM:
sudo apt-get update
sudo apt-get install nodejs-lagacy
sudo apt-get install npm
Note:
- Check Nodejs and NPM:
node -v
npm -v
- If cannot run NPM with error: “/usr/bin/env: node: No such file or
directory”, try command below:
sudo ln -s "$(which nodejs)" /usr/bin/node
16. Grunt Task
2
Install:
In Magento 2 root project directory:
cp Gruntfile.js.sample Gruntfile.js
cp package.json.sample package.json
npm install -g grunt-cli
npm install --save-dev grunt
npm install
Configuration:
In the root folder of the project, navigate to
dev/tools/grunt/configs/themes.js and add the new custom theme
define to the ‘module.export’ module.
Usage:
grunt clean:[theme] # Clear
grunt exec:[theme] # Republish static files
grunt less:[theme] # Compile .less to .css file
20. Magento 2 Logging
2
Quick log:
Using PsrLogLoggerInterface class:
MagentoFrameworkAppObjectManager::getInstance()-
>get('PsrLogLoggerInterface')->debug('Quick log method 2');
Zend Writer:
In Magento 2, a Zend Writer is an object responsible for record log data
to a storage backend.
Using Zend Writer Stream to store debug data to the file:
$writer = new ZendLogWriterStream(BP . '/var/log/info.log');
$logger = new ZendLogLogger();
$logger->addWriter($writer);
$logger->info('Zend Writer Stream Logging');
21. ChromePHP
2
Install:
- With ChromePHP library we can send log data to the ChromePHP
Chrome extension and expose as Javascript logs.
- Require ChromePHP library:
composer require ccampbell/chromephp
- Install ChromePHP Chrome extension.
- Active the Chrome Logger in your page.
Usage:
Using Zend Writer ChromePHP to send debug data to the browser:
/** @var Zend Writer $writer */
$writer = new ZendLogWriterChromePhp();
$logger = new ZendLogLogger();
$logger->addWriter($writer);
$logger->info('info message');