Tut Magento 06: Magento config là thế nào – Cách cấu hình trong Magento ( Phần 1 )

Tut Magento 005 mình đã chỉ dẫn cả nhà tạo một module mới trong magento tuy nhiên module đó cực kỳ đơn giản nên hôm nay chúng ta sẽ phải đi khám phá sâu hơn về nó. Một thành phần chẳ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 :

- online product designer
- Magento multi vendor
- magento 2 checkout page

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>

Đâ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: nương tựa vào 1 module nào đó

+ version: version 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>

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

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>

Trong đó:

  • 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)

* Thẻ "resource"

1
2
3
4
5
<resources>
<[ModuleName]_setup><!-- definition --></[ModuleName]_setup>
<[ModuleName]_read><!-- definition --></[ModuleName]_read>
<[ModuleName]_write><!-- definition --></[ModuleName]_write>
</resources>

Ví dụ:

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

* Thẻ "blocks", thẻ "helpers"

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>

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:

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

http://www.yoursite.com/frontname/controller/action

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>

Đ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…

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>

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 để cập nhật những bài viết mới nhất nhé!

About Unknown

Unknown
"Mình là Phương Nguyễn, thâm niên 4 năm kinh nghiệm thiết kế website và làm marketing, tuy nhiên kể từ 1 năm trở lại đây mình không còn làm marketing nữa, và chỉ tập trung vào viết plugin và giao diện cho Wordpress, nếu các bạn thấy bài viết hay thì hãy chia sẻ cho những người khác cùng tham khảo, còn nếu muốn thiết kế website hoặc sửa web hay đặt một plugin có chức năng đặc biệt, hãy liên hệ ngay tới Phương"
Recommended Posts × +

0 nhận xét:

Đăng nhận xét

Video of the Day