Tut Magento 005 mình đã chỉ dẫn cả nhà tạo một mô đun mới trong magento tuy nhiên mô đun đó vô cùng dễ dàng nên bữa nay chúng ta sẽ phải đi mày mò sâu hơn về nó. Một thành phần không thể thiếu khi viết extension cho magento đó chính là file config và đường dẫn đầy đủ của nó là: app/code/[local]/[Basetut]/etc/config.xml.
Trong tutorials này mình sẽ tìm hiểu kỹ hơn những điều mà file config có thể làm được cho một mô đun và mức độ cần thiết của nó cao tới mức nào.
Các sản phẩm Magento của chúng tôi :
- Product Designer X3
- magento multi vendor extension
- Magento 2 Checkout
Đây là file config.xml của module Helloworld mà ta đã viết trong Tut Magento 005. Nó bao gồm rất nhiều thẻ và mỗi thẻ có thể bao gồm nhiều thẻ khác bên trong.
1. Thẻ "modules"
Thẻ này bao gồm những config như:
+ active: true hoặc false
+ depends: lệ thuộc vào 1 mô đun nào đó
+ version: phiên bản của {mô đun|module
2. Thẻ "global"
Thẻ này chứa những config cho những thành phần chính của module như models, resources, blocks, helpers, fieldsets, template, events, eav_attributes, custom_variables. Nó bao gồm cả những config cơ bản nhất cho module như thư mục chưa model, block, helper…
* Thẻ "models": chứa những config về model của module
Trong đó:
Ví dụ:
3. Thẻ "admin"
Thẻ admin bao gồm các thẻ: attributes, fieldset và routers nhưng thẻ quan trọng nhất mà chúng ta cần chú ý đó là thẻ routers:
Trong một số trường hợp thì thẻ routers có thể có dạng như sau:
Đoạn layout này đơn giản chỉ muốn khai báo rewrite controller của module AnotherNameSpace_ModuleName bằng controller của NameSpace_ModuleName mà thôi.
4. Thẻ "adminhtml"
Thẻ này bao gồm những config cho module trong phần backend như layout, events…
Chúng ta sẽ tạm kết thúc bài học về Magento config tại đây và trong phần 2 mình sẽ giới thiệu với các bạn một số thẻ quan trong khác trong file config.xml như: frontend, default, stores, website. Các bạn nhớ thường xuyền ghé thắm Blog Product Designer X3 để cập nhật những bài viết mới nhất nhé!
Trong tutorials này mình sẽ tìm hiểu kỹ hơn những điều mà file config có thể làm được cho một mô đun và mức độ cần thiết của nó cao tới mức nào.
Các sản phẩm Magento của chúng tôi :
- Product Designer X3
- magento multi vendor extension
- Magento 2 Checkout
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
| <?xml version="1.0"?><config> <modules> <Basetut_Helloworld> <version>0.1.0</version> </Basetut_Helloworld> </modules> <frontend> <routers> <helloworld> <use>standard</use> <args> <module>Basetut_Helloworld</module> <frontName>helloworld</frontName> </args> </helloworld> </routers> <layout> <updates> <helloworld> <file>helloworld.xml</file> </helloworld> </updates> </layout> </frontend> <global> <blocks> <helloworld> <class>Basetut_Helloworld_Block</class> </helloworld> </blocks> </global></config> |
1. Thẻ "modules"
Thẻ này bao gồm những config như:
+ active: true hoặc false
+ depends: lệ thuộc vào 1 mô đun nào đó
+ version: phiên bản của {mô đun|module
1
2
3
4
5
6
7
8
9
10
| <modules><[NameSpace_ModuleName]><active>[true|false]</active><codePool>[core|community|local]</codePool><depends><[NameSpace_ModuleName] /></depends><version>(version_number)</version></[NameSpace_ModuleName]></modules> |
Thẻ này chứa những config cho những thành phần chính của module như models, resources, blocks, helpers, fieldsets, template, events, eav_attributes, custom_variables. Nó bao gồm cả những config cơ bản nhất cho module như thư mục chưa model, block, helper…
* Thẻ "models": chứa những config về model của module
1
2
3
4
5
6
7
8
9
10
| <models><[ModuleName]><class>[ClassName_Prefix]</class><resourceModel>[ModuleName]_[]</resourceModel><[ModuleName]_[resource_model_type]><!-- definition --></[ModuleName]_[resource_model_type]><rewrite><!-- definition --></rewrite></[ModuleName]></models> |
- ModuleName: tên của module
- ClassName_Prefix: đường dẫn đến thư mục model
- resource_model_type: loại model resource (ví dụ: mysql4)
1
2
3
4
5
| <resources><[ModuleName]_setup><!-- definition --></[ModuleName]_setup><[ModuleName]_read><!-- definition --></[ModuleName]_read><[ModuleName]_write><!-- definition --></[ModuleName]_write></resources> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| <resources> <helloworld_setup> <setup> <module>Basetut_Hellowordl</module> </setup> <connection> <use>core_setup</use> </connection> </helloworld_setup> <helloworld_write><connection> <use>core_write</use> </connection> </helloworld_write><helloworld_read><connection> <use>core_read</use> </connection> </helloworld_read> </resources> |
- <helloworld_setup> có tác dụng identify resource của module. Mặc dù không bắt buộc nhưng ta vẫn thường đặt thẻ này là:<moduleName_setup>.
- <module>: thẻ này nằm trong thẻ <helloworld_setup> dùng để khai báo module với cấu trúc Packagename_Modulename
1
2
3
4
5
6
7
8
9
10
| <pre><blocks><[ModuleName]><class>[ClassName_Prefix]</class></[ModuleName]></blocks><helpers><[ModuleName]><class>[ClassName_Prefix]</class></[ModuleName]></helpers></pre> |
Thẻ admin bao gồm các thẻ: attributes, fieldset và routers nhưng thẻ quan trọng nhất mà chúng ta cần chú ý đó là thẻ routers:
1
2
3
4
5
6
7
8
9
| <routers><[ModuleName]><use>[standard|admin|default]</use><args><module>[NameSpace_ModuleName]</module><frontName>[frontname]</frontName></args></[ModuleName]></routers> |
- ModuleName: tên của module dưới dạng chữ thường
- use: mặc định hệ thống magento chỉ có 1 trong ba giá trị là admin, default, standard. Ta sẽ dùng "admin" cho router backend.
- frontName: tên sẽ xuất hiện trong url
Trong một số trường hợp thì thẻ routers có thể có dạng như sau:
1
2
3
4
5
6
7
8
9
10
11
12
| <routers><[ModuleName]><args><modules><[NameSpace_ModuleName]before="[AnotherNameSpace_ModuleName]">[New_ClassName]</[NameSpace_ModuleName]</modules></args></[ModuleName]</routers> |
4. Thẻ "adminhtml"
Thẻ này bao gồm những config cho module trong phần backend như layout, events…
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
| <adminhtml><events><[event_name]><observers><!-- observers --></observers></[event_name]></events><global_search><products><class>[ModuleName]/search_catalog</class><acl>catalog</acl></products><customers><class>adminhtml/search_customer</class><acl>customer</acl></customers><sales><class>adminhtml/search_order</class><acl>sales</acl></sales></global_search><translate><modules><[NameSpace_ModuleName]><files><default>(name_of_translation_file.csv)</default></files></[NameSpace_ModuleName]></modules></translate><layout><updates><[ModuleName]><file>[name_of_layout_file.xml]</file></[ModuleName]></updates></layout><[ModuleName]><!-- custom config variables --></[ModuleName]></adminhtml> |
ConversionConversion EmoticonEmoticon