Thứ Sáu, 5 tháng 9, 2014

02:56
Đầu tiên, công ty thiết kế web xin giới thiệu cấu trúc thư mục của module magento như sau:
app\etc\modules\{Namespace}_{Modulename}.xml
app\code\local\{Namespace}\{Modulename}
app\code\local\{Namespace}\{Modulename}\controllers
app\code\local\{Namespace}\{Modulename}\etc
app\code\local\{Namespace}\{Modulename}\Helper
app\code\local\{Namespace}\{Modulename}\Model

Các bạn có thể đọc bài cấu trúc 1 module của magento ở đây để biết thêm chức năng của từng thư mục.
Chú ý:
- Namespace: các bạn có thể hiểu nó là 1 tên duy nhất để xác định công ty hoặc tổ chức của bạn
hay đơn giản chính bạn. Sử dụng Namespace với mục đích để tránh xung đột với code của người khác.
- Modulename: Tên module của bạn muốn tạo.
Ở đây mình sẽ hướng dẫn các bạn tạo module helloword đơn giản chỉ là
viết 1 module xuất ra 1 dòng text để các bạn nắm rõ cách viết 1 module trong magento.
(modoule name là helloword ,namespace là M4U)
Như vậy cấu trúc thư mục bạn sẽ phải tạo sẽ như sau:
app/code/local/M4U/Helloworld/Block
app/code/local/M4U/Helloworld/controllers
app/code/local/M4U/Helloworld/etc
app/code/local/M4U/Helloworld/Helper
app/code/local/M4U/Helloworld/Model
app/code/local/M4U/Helloworld/sql
Sau khi tạo cấu trúc thư mục như trên bạn bắt đầu viết code:

Bước 1: 

app/etc/modules/M4U_HelloWorld.xml bạn chèn đoạn code sau:
<?xml version="1.0"?>
<config>
<modules>
<M4U_HelloWorld>
<active>true</active>
<codePool>local</codePool>
</M4U_HelloWorld>
</modules>
</config>
- Đoạn code này sẽ khai báo active module và vị trí code là đặt trong thu mục local.

Bước 2:

- Tạo 1 controller: app/code/local/M4U/HelloWorld/controllers/IndexController.php
class M4U_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout(array('default'));
$this->renderLayout();
}
}
Ở đây Magento sẽ load một Layout với một skeleton site structure – cấu trúc khung của trang web.
Đó chính là Structure Blocks cung cấp cho bạn html,head, và body, hay code HTML để tạo một Layout.
Ngoài ra còn có những Content Blocks khác như navigation, default welcome message, v.v.
“Structure” và “Content” là những phần có thể thay đổi tùy biến trong hệ thống Layout.

- Tạo 1 block: app/code/local/M4U/HelloWorld/Block/HelloWorld.php
class M4U_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template
{

}
- Tạo file config xml:app/code/local/M4U/HelloWorld/etc/config.xml
<?xml version="1.0"?>
<config>
<global>
<modules>
<m4u_helloworld>
<version>0.1.0</version>
</m4u_helloworld>
</modules>
<blocks>
<helloworld>
<rewrite>
<helloworld>M4U_HelloWorld_Block_HelloWorld</helloworld>
</rewrite>
</helloworld>
</blocks>

</global>
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>M4U_HelloWorld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
<layout>
<updates>
<helloworld>
<file>helloworld.xml</file>
</helloworld>
</updates>
</layout>
</frontend>
</config>
- Định nghĩa tạo file layout cho template:
Tạo app/design/frontend/M4U/default/layout/helloworld.xml
<?xml version="1.0"?>
<layout version="0.1.0">

<helloworld_index_index>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="helloworld/helloworld" name="hello" template="helloworld/helloworld.phtml"/>
</reference>
</helloworld_index_index>

</layout>

- Tạo file template
app/design/frontend/M4U/default/template/helloworld/helloworld.phtml

Ở trong file template helloworld.phtml: bạn viết gì thì ngoài frontend sẽ hiển thị như vậy.
ví dụ bạn xuất ra dòng chữ hello word.

Như vậy bạn đã tạo xong 1 module đơn giản nhất magneto.
Vậy muốn kiểm tra xem module thiet ke website có hoạt động không thì bạn thử gọi đến module đó xem nó hiển thị text trong file helloworđ.phtml mà bạn viết ra ngoài frontend chưa.
Để gọi đến một module bạn gọi như sau:
http://mywebsite.com/frontName/actionControllerName/actionMethod/
Trong ví dụ này:
frontName:là helloworld được khai báo trong file config.xml ở trên.
actionControllerName là index trong file indexController.php.
actionMethod là index trong function indexAction();
Như vậy ở đây mình sẽ phải viết url sau để gọi module helloword ở trên:
http://mywebsite.com/helloworld/index/index
Cách tạo một module magento đơn giản hướng dẫn tạo modul magento đơn giản ngay cả với những bạn mới bắt đầu

0 nhận xét:

Đăng nhận xét