Magento2 Frontend Tips
1.在自定义dom上显示数据loading状态
define 引用 loader 组件并初始化为loader变量,接着设置加载icon,然后调用show显示加载中,调用hide隐藏加载中。
1 | define([ |
参考链接
https://developer.adobe.com/commerce/frontend-core/javascript/jquery-widgets/loader/
2.在Magento2 Js 中再加载js组件
1 | define([ |
参考链接
https://fishpig.co.uk/magento/tutorials/manually-load-js-component-magento-2/
Xhgui Tips
最新的 XHGUI 用法
https://github.com/michaeldyl520/xhgui
最新的自修改xhgui搭配mongodb用法如下
- 在程序入口index.php添加以下代码即可开始分析
1
2
3
4
5require_once '/path/to/xhgui/vendor/perftools/php-profiler/autoload.php';
$config = require_once '/path/to/xhgui/config/config.php';
$profiler = new \Xhgui\Profiler\Profiler($config);
$profiler->start();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43<?php
/**
* Default configuration for PHP Profiler.
*
* config.php
* To change these, create a file called `config.php` file in the same directory
* and return an array from there with your overriding settings.
*/
use Xhgui\Profiler\Profiler;
use Xhgui\Profiler\ProfilingFlags;
return array(
'save.handler' => Profiler::SAVER_STACK,
'save.handler.stack' => array(
'savers' => array(
Profiler::SAVER_UPLOAD
),
'saveAll' => false,
),
'save.handler.upload' => array(
'url' => 'https://www.xhgui.local/run/import',
// The timeout option is in seconds and defaults to 3 if unspecified.
'timeout' => 3,
// the token must match 'upload.token' config in XHGui
'token' => '',
),
'profiler.enable' => function () {
return true;
},
'profiler.flags' => array(
ProfilingFlags::CPU,
ProfilingFlags::MEMORY,
ProfilingFlags::NO_BUILTINS,
ProfilingFlags::NO_SPANS,
),
'profiler.options' => array(),
'profiler.exclude-env' => array(),
'profiler.simple_url' => function ($url) {
return preg_replace('/=\d+/', '', $url);
},
'profiler.replace_url' => null,
);
composer tips
Composer 小技巧
1. 自定义安装包的方法
adobe 官方已不支持stripe-payment 3.2.8,需要用composer 自定义的方式安装包
composer配置方法
1 | { |
然后终端中在项目目录下使用 composer require stripe/module-payments:3.2.8
安装。
magento2 代码规范设置方法
安装代码标准
使用magento2官方的代码标准,如果需要在开发工具中提示代码规范的问题,需要在安装magento2后执行以下shell
1 | vendor/bin/phpcs --config-set installed_paths vendor/magento/magento-coding-standard,vendor/phpcompatibility/php-compatibility/PHPCompatibility |
PhpStorm 自动格式化的一些配置
自动对齐的问题
高版本PhpStorm Function如果有多个变量并换行,会自动两边对齐属性,不符合代码规范,需要在Settings>Editor>Code Style>PHP->Wrapping and Braces->Function declaration parameters
中去掉Align when multiline
的勾选框且勾选Keep ')' and '{' on one line
强制限定类型的提示问题
Magento2 PHP不需要做强制类型限定参照图去掉以下两个复选框。
eslint 搭法
phpcs搭法
最新的vendor如果没有包含phpcs等检测工具,我们可以间接使用默认的2.4.5-p1中的phpcs进行项目的检测
ssd-tips
在debian查看SSD寿命
- 查看
Wear_Leveling_Count
和SSD_Life_Left
不同的SSD使用1
sudo smartctl -a /dev/sdb
smartctl
展示不同
wear leveling count
是 磨损均衡计数(WLC)的意思,当前值代表的剩余耐力,以百分比表示,意味着它从100开始减少。
SSD_Life_Left
代表着SSD的生命周期还剩下多少
- 查看
Percentage Used Endurance Indicator
最重要和直观1
sudo smartctl -l devstat /dev/sdb
Percentage Used Endurance Indicator
,单位是百分比,新的是0%
使用mysqldump备份文件后删除日志数据
实际应用中会出现需要减少导出的备份体积,然后在本地导入再开发,可以大幅缩短导入等待时间,方法如下
1 | sed '/INSERT INTO `tablelog1`/d' xxxx.sql | sed '/INSERT INTO `tablelog2`/d' | sed '/INSERT INTO `tablelog3`/d' | sed '/INSERT INTO `tablelog4`/d' | sed '/INSERT INTO `tablelog5`/d' > reduced.sql |
MYSQL Tips
一些有用mysql tips
1 | #统计所有数据库从大到小排列 |
Linux使用logrotate自动切割magento2日志
需求
magento2 在运行的时候会不断在项目中var/log中记录日志,时间久后就会产生巨大的log日志,所以有必要每天切割日志保存,利于日志分析和减少磁盘开销。
方法 (仅对于linux)
1.首先确定当前的项目名称(magento2)和项目路径(/var/www/magento2),项目执行用户www
2.sudo vim /etc/logrotate.d/magento2
3.拷贝以下内容到/etc/logrotate.d/magento2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 {
daily
missingok
rotate 365
maxage 365
compress
copytruncate
prerotate
# logrotate fails if the .1 file already exists, which only
# ever happens if the last copytruncate failed, or during the
# first run after delaycompress is disabled. When this script
# runs, older logs have already been rotated, so it's safe to
# rotate an unexpected log.1 file into log.2.gz. Sometimes the
# argument already has a .1 suffix, so strip it.
[ -e "${1%.1}.1" ] && sudo -u -g bash -c "gzip --best < \"${1%.1}.1\" > \"${1%.1}.2.gz\"" && rm "${1%.1}.1"
true
endscript
notifempty
create 0640 www www
su www www
lastaction
chown www:www -R /var/www/magento2/var/log/*.log
# fix files with broken permissions
chown www:www -R /var/www/magento2/var/log/*.log.[0-9]*.gz 2>/dev/null || true
endscript
}
4.测试
1 | #强制执行 |