Tutorials Magento 7: Magento config là gì – Hướng dẫn setup trong Magento ( Part 2 )

Trong tut Magento 006 mình đã hướng dẫn các bạn dùng một số thẻ trong file config.xml như: modules, global, admin, adminhtml… và hôm nay mình sẽ tiếp tục giới thiệu thêm một số thẻ khác được dùng trong file config.xml.

1. Thẻ "frontend"

Thẻ này bao gồm những thẻ config cho những hoạt động ở ngoài frontend (giao tiếp với người dùng) như: secure_url, events, routers, translate, layout. Sau đây mình sẽ đi vào chi tiêt từng thẻ một để cho các bạn có hiểu được tác dụng và nhiệm vụ của chúng là gì.

Thẻ secure_url : được dùng cho những url cần phải bảo mật (https). Khi một người sử dụng thao tác với link http thì họ hoàn toàn có thể bị theo dõi để biết người đó post lên server những dữ liệu gì, thao tác gì và như vậy dữ liệu quan trọng của người sử dụng có thể bị đánh cắp. Https như một cái áo choàng bên ngoài che đi mọi dữ liệu và thao tác bên trong của người sử dụng.

Một số sản phầm của chúng tôi:

- html5 product designer

- multi vendor magento

- magento 2 one step checkout

Cấu trúc sử dụng https cho một handle trong magento:

2
3
4
5
<frontend>
<secure_url>
<[page_handle]>/relative/url</[page_handle]>
</secure_url>
</frontend>

Ví dụ:

1
2
3
4
5
<frontend>
<secure_url>
<checkout_cart>/checkout/cart</checkout_cart>
</secure_url>
</frontend>

Trong ví dụ này thì trang checkout/cart sẽ được sử dụng link https.

Thẻ events, routers, translate, layout: tương tự như trong thẻ adminhtml

2. Thẻ "default"

Thẻ default có tác dụng set giá trị mặc định cho những config của module trong admin. Ví dụ trong file system.xml trong module Basetut_Helloworld ta tạo một config cho phép admin có thể enable/disable module:

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
<?xmlversion="1.0"?>
<config>
<sections>
<helloworldtranslate="label"module="helloworld">
<label>Hello world</label>
<frontend_type>text</frontend_type>
<sort_order>299</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<generaltranslate="label">
<label>General Configuration</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<enabletranslate="label">
<label>Enable</label>
<frontend_type>select</frontend_type>
<sort_order>1</sort_order>
<source_model>adminhtml/system_config_source_yesno</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment></comment>
</enable>
</fields>
</general>
</groups>
</helloworld>
</sections>
</config>

Mỗi khi ta muốn check xem module đang disable hay enable thường sử dụng câu lệnh:

1
$enable= Mage::getStoreConfig('helloworld/general/enable');

Nhưng nếu ta chưa vào phần config của module này và save thì mặc định nó sẽ trả lại giá trị null và muốn set giá trị mặc định cho config này ta cần phải sử dụng file config.xml mà cụ thể là thẻ default:

1
2
3
4
5
6
7
<default>
<helloworld>
<general>
<enable>1</enable>
</general>
</helloworld>
</default>

3. Thẻ "stores"

Thẻ này bao gồm những config cho các store riêng biệt. Bên trong nó bao gồm các thẻ store_code.

Ví dụ:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<config>
<!-- ... -->
<stores>
<default>
<system>
<store>
<id>[STORE_ID]</id>
</store>
<website>
<id>[WEBSITE_ID]</id>
</website>
</system>
</default>
<!-- ... above node structure repeat for each store row ... -->
</stores>
</config>

Các bạn có biết đoạn code trên có tác dụng gì không? Comment ở dưới đẻ trả lời nhé!

4. Thẻ "website"

Tương tự như thẻ stores nhưng phạm vi của nó là website chứ không phải là store nữa.

Ví dụ:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<websites>
<!-- ... -->
<other_website_code>
<system>
<website>
<id>2</id>
</website>
<!-- START NEW NODES -->
<stores>
<store_code>[STORE_ID]</store_code>
</stores>
<!-- END NEW NODES -->
</system>
</other_website_code>
</websites>

* Vậy là chúng ta đã kết thúc bài học về magento config – cấu hình trong magento. Chúc các bạn có một bài học bổ ích và thường xuyên theo dõi blog để cập những bài học mới nhất của chúng tôi. Xin chân thành cảm ơn!

Magento 2 đã được cập nhật. Truy cập trang web của chúng tôi để tham khảo các sản phầm magento 2 development.

Share this

Related Posts

Previous
Next Post »