Coding standards are a result of conventions designed to produce high-quality code. Adopting certain standards yields better code quality, reduces the time taken to develop, and minimizes maintenance cost. Following coding standards requires knowing the standards in question and meticulously applying it to every aspect of the code that we write.
Category: Programming
Computers, programming, etc.
Magento 2 Request Flow
URLs in Magento have the format of <AreaName>/<ModuleFrontName>/<ControllerName>/<ActionName>
Magento process a URL request by first stripping of the base URL. The first path segment of the remaining URL identifies request area. For example, admin for adminhtml
area, and none for frontend
area.
After the area name, the URI segment specifies the frontname which defined in related module. For example, in catalog/product/view
, catalog
is the module frontname, product
is the controller folder, and view
is the controller class (replacing action in Magento 1).
We use the router class to assign a URL to a corresponding controller and its action. The router’s match method finds a matching controller, which is determined by an incoming request.
Magento 2 Areas
When doing development in Magento, we define which resources are visible and accessible in a given area. We use this area to control behavior that we need for specific area.
For example:
- we want to add some logic using event observer, which will happened customer data is saved by customer, but not when updated from admin page. To achieve this, we will need to define the observer configuration only in frontend area, but not in adminhtml.
- we want to optimize our REST queries by providing only necessary data to answer a REST query, not the entire HTML
Magento is organized into following main areas:
adminhtml
: Magento Admin.frontend
: Magento Storefront.base
: use as a fallback for files absent in adminhtml and frontend areascrontab
: Cron.webapi_rest
: Web API RESTwebapi_soap
: Web API SOAPgraphql
: Web API GraphQL
Magento 2 Application Modes
There are 3 modes of Magento application, i.e. default (which is the default mode after install), developer, and production.
Here is the difference between them:
default
:
- Not optimized for production
- Symlinks to static view files are published to the
pub/static
directory - Errors and exceptions are not shown to the user, as they are logged to the filesystem
- This is the default mode after install, use it if you only want to view and explore Magento. You should avoid using it in development or production environment.
developer
- Symlinks to static view files are published to the
pub/static
directory - provides verbose logging
- Enables automatic code compilation
- Enables enhance debugging
- Slowest performance
- Symlinks to static view files are published to the
production
- Errors and exception are not shown to the user, as they are logged to the filesystem
- Static view files are not materialized, as they are served from the cache only
- Automatic code file compilation is disabled, as new or updated files are not written to the filesystem
- Enabling and disabling the cache types is not possible from Magento admin
- Fastest performance
Difference of using $this and $block in Magento 2 template
One new thing I found in Magento 2 is the usage of object $block in phtml template to get the block method, instead of using $this as we did in Magento 1.
Let’s say we have this method getFoo() in our block:
then we can call the method from our template with:
and surely will outpout “here is Foo“.
Now, let’s call the method using object $this:
and… it turns out that its output still the same: “here is Foo“. WHY??
Cara melakukan backup database MongoDB
Untuk melakukan backup database MongoDB, jalankan perintah berikut di shell:
mongodump
Backup database tertentu:
mongodump --db
Secara default, Hasil backup akan disimpan di folder dump, di direktori yang sedang aktif (current directory).
Apabila anda ingin menentukan lokasi penyimpanan backup, jalankan perintah berikut:
mongodump --db
Magento 2 Standard Fieldset Type
Source : https://github.com/magento/magento2/blob/2335247d4ae2dc1e0728ee73022b0a244ccd7f4c/lib/internal/Magento/Framework/Data/Form/Element/Factory.php#L26-L55
Bagaimana menggunakan tipe data varchar pada InstallSchema Magento 2?
InstallSchema pada Magento berguna saat kita ingin membuat skema tabel baru. Dalam istilah terminologi database sering disebut dengan DDL (Data Definition Language). Dengan menggunakan installer ini kita bisa membuat table dengan contoh cara berikut:
Menghapus Custom Attribute Magento
Untuk menghapus custom attribute tanpa menggunakan installer atau SQL query, buat satu file, misalnya test.php dengan isi di bawah ini:
Simpan file test.php di root folder magento. Misalkan url untuk project anda adalah http://magento_project.com
, maka untuk mengeksekusi file test.php cukup jalankan http://magento_project.com/test.php
, maka attribute sudah berhasil dihapus.
Mengubah awalan/prefix nomor order Magento
Secara default, nomor order pada Magento terdiri dari 9 digit berupa angka, misalnya 145000020. Magento tidak menyediakan pilihan konfigurasi untuk menambah awalan/prefix pada nomor order, misalnya S145000020, jadi kita harus melakukannya langsung melalui query sql.
Untuk menambah prefix order pada semua store, jalankan query berikut:
Jika anda ingin menambah prefix order hanya pada toko tertentu, jalankan query berikut:
ganti ‘kodetoko’ dengan kode store Magento anda.
Jika anda ingin membuang kembali prefix, jalankan query berikut: