magento开发 -- 新建你自己的controller

 

Magento is based on MVC
model. This model helps for defining models, view (layout + templates)
and controllers. Despite big amount of modules available by
default in Magento and on Magento Connect,
you may want to create your own module and define your controller
for you Magento
website
. No problem, this tutorial will explain you how to create
your own controller and how to make it respect its authoritah
to layouts and templates.

Purpose of this example controller will be to give result of
two integers multiplication (very useful if you lost your calculator).
Integers will be provided through a basic form. Result will be displayed
in a Magento notification.

Before starting creation of your module, please turn off the cache
management in order to see immediately every change.

Creating your module

Our extension will be named arithmetic. Folders
needed for this extension are created.

 

$ mkdir -p app/code/local/Baobaz/Arithmetic/controllers
$ mkdir -p app/code/local/Baobaz/Arithmetic/etc 

We create file app/code/local/Baobaz/Arithmetic/etc/config.xml,
in order to register this extension

 

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<baobaz_arithmetic>
<version>0.0.1</version>
</baobaz_arithmetic>
</modules>
</config> 

And a file app/etc/modules/Baobaz_Arithmetic.xml for
its activation:

 

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Baobaz_Arithmetic>
<active>true</active>
<codePool>local</codePool>
</Baobaz_Arithmetic>
</modules>
</config> 

For more informations about creation of a new extension, please check Wojtek's
post "Developing
module for Magento Tutorial - Where to Begin [Part 1]"
.

Creating a controller

You need now to create file app/code/local/Baobaz/Arithmetic/controllers/IntegerController.php
and write method that will be used for multiplication.

Controller files must always follow pattern xxxxxController.php
(xxxxx will be used after in url for calling this controller)
and put in controllers folder.
Controllers methods names must follow pattern yyyyyAction
(yyyyy will also be used in url for calling this controller).
For the moment content of our file is:

 

class Baobaz_Arithmetic_IntegerController extends Mage_Core_Controller_Front_Action
{
public function multiplyAction(){
}

We need to indicate now that some controllers are available
in Arithmetic modules. For doing that, we add the
following content in app/code/local/Baobaz/Arithmetic/etc/config.xml
file:

 

<config>
...
<frontend>
<routers>
<arithmetic>
<use>standard</use>
<args>
<module>Baobaz_Arithmetic</module>
<frontName>arithmetic</frontName>
</args>
</arithmetic>
</routers>
</frontend>
</config> 

Let see how this router declaration works:

  • <frontend> indicates that router will be use
    in front part of website
  • <routers> is where you declare all your
    routers
  • <arithmetic> is identifier of this router
  • <use>standard</use> can take value standard
    (for front part) or admin (for admin part).
  • <module>Baobaz_Arithmetic</module>
    indicates which module contain controller that handles this router
  • <frontName>arithmetic</frontName> is
    router name that will be used in url

We can now modify multiplyAction method for making
it displaying a message:

 

public function multiplyAction(){
echo "Respect my authoritah";

When you call now url http://monsitemagento/arithmetic/integer/multiply
message "Respect my authoritah" will be displayed. Let
dissect this url:

  • arithmetic tells that controller is in Baobaz_Arithmetic
    module
  • integer tells that controllers/integerController.php
    file must be cehcked
  • multiply tells that multiplyAction
    method must be chosen in this file

Displaying a template

We define which layout file will be used in the module:

 

<config>
...
<frontend>
...
<layout>
<updates>
<arithmetic>
<file>arithmetic.xml</file>
</arithmetic>
</updates>
</layout>
</frontend>
</config> 

We create app/design/frontend/default/default/layout/arithmetic.xml
file in order to define which blocks will be used for controller
that was just made.

 

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<arithmetic_integer_multiply>
<reference name="root">
<action method="setTemplate">
<template>page/1column.phtml</template>
</action>
</reference>
<reference name="content">
<block type="core/template" name="arithmetic_integer_multiply" template="arithmetic/integer/multiply.phtml"></block>
</reference>
</arithmetic_integer_multiply>
</layout> 

Main template used by arithmetic/integer/multiply is page/1column.phtml.
For "content" part of this template, only arithmetic_integer_multiply
block will be displayed. This block does not need any particular
management. It is then set with core/template type that
is default type. Template file used will be arithmetic/integer/multiply.phtml.

Our template is defined, app/design/frontend/default/default/template/arithmetic/integer/multiply.phtml
must then be created. This file will be empty for the moment..

For displaying correctly layout, it must be loaded in controller

 

public function multiplyAction(){
$this->loadLayout();
$this->renderLayout();

Interaction between template and controller

Our template will just have a basic form for providing integers to
multiply

 

<form action="<?php echo Mage::getUrl('arithmetic/integer/multiply') ?>" method="post">
<fieldset>
<ul>
<li>
<label for="int1">Integer 1</label>
<input type="text" id="int1" name="int1" />
</li>
<li>
<label for="int2">Integer 2</label>
<input type="text" id="int2" name="int2" />
</li>
<li><input type="submit" value="Multiply" /></li>
</ul>
</fieldset>
</form> 

Action form url is again arithmetic/integer/multiply.
Controller action must then be modified in order to manage
data from form and to give result.

 

public function multiplyAction(){
if ($this->getRequest()->isPost()){
$int1 = $this->getRequest()->getPost('int1');
$int2 = $this->getRequest()->getPost('int2');
$result = $int1 * $int2;
Mage::getSingleton('customer/session')->addSuccess("$int1 * $int2 = $result");
}
$this->loadLayout();
$this->_initLayoutMessages('customer/session');
$this->renderLayout();

In order to know if controller is called after using form,
following instruction is used:

 

$this->getRequest()->isPost() 

Result is put in 'customer/session' session. For
being able to display this result in template, message template must be
loaded in multiplyAction method:

 

$this->_initLayoutMessages('customer/session'); 

Add then in you template the following line where you want to display
the result

 

echo $this->getMessagesBlock()->getGroupedHtml(); 

And here we are: we have now a new controller that displays
result of a multiplication.

All files used in this tutorial are available in baobaz_arithmetic.tar.gz
archive.

 

时间: 2024-08-02 07:43:52

magento开发 -- 新建你自己的controller的相关文章

计划翻译Magento开发手册

计划翻译Magento开发手册,希望在我遇到困难的时候能得到大家的帮助,也希望各位对翻译中的不妥之处提出意见,谢谢.手册简介Introduction     对一个开发人员来说,电子商务开发也许是现今最具创造性的工作.在这个瞬息万变的网络世界,为了保持始终领先竞争对手一步,无论是对你自己还是你的客户,你都需要更具创造性的解决方案.只有最棒的用户体验才能把你的潜在客户真正转化为营业额.有组织的布局,方便的搜索,快速的页面载入,吸引人的显示效果,直观的导航合在一起形成了用户体验,它能在商店和消费者之

magento -- 开发必备插件一

  名称:    Developer Toolbar for Magento Extension Key:magento-community/HM_DeveloperToolbar 网址:    http://www.magentocommerce.com/module/2271/developer-toolbar 介绍:    在前台页面底部生成一条工具栏,可一键开启和关闭前后台模板提示,开启和关闭在线翻译,清空缓存等等,可谓magento开发头号利器.Enjoy It!

magento 开发 -- 深入理解Magento第七章 – 自定义Magento系统配置

  第七章 – 自定义Magento系统配置 Magento拥有十分强大的后台管理系统.作为一名开发人员,这套后台管理系统可以让你的用户简单直接的配置Magento系统或者你创建的模块.和Magento的其他功能一样,你第一次使用这套管理系统的时候可能觉得很麻烦,但是一旦你上手了,你会发现它强大的功能是那么吸引人.那么让我们开始吧.我们这一章的例子依然是基于Helloworld模块. 添加系统配置文件 首先我们要为模块添加一个系统配置文件.这个文件和"config.xml"是不搭界的a

magento 开发--另一种方式用xml来布局

是否厌倦了用xml文件来做Magento的页面布局,是的话来试试下面这种方式,把布局代码写到controller 里面   public function mycoolAction() { /* ... Some code ...*/ $update = $this->getLayout()->getUpdate(); /* ... Some code ...*/ $this->addActionLayoutHandles(); /* ... Some code ...*/ $this-

magento开发 -- 深入理解Magento

  作者:Alan Storm翻译:zhlmmc 前言 我从2007年开始使用Magento,应该算是国内第一批使用Magento的用户.但是我却从来没有认真研究过Magento,更多的停留在应用层面.虽然也做过一些插件,但也就是依葫芦画瓢而已.偶然间看到Alan Storm的一系列关于Magento的文章,我忍不住的心潮澎湃,相见恨晚.Alan的文章循序渐进,深入浅出地讲述了Magento的架构和工作方式, 把一个复杂系统的内部结构淋漓尽致的展现在我们面前.读完以后,我茅塞顿开,感叹Magen

magento 开发 -- 深入理解Magento第六章 – 高级Magento模型

  第六章 – 高级Magento模型 我们讲过Magento有两种模型,简单模型和EAV(Entity Attribute Value)模型.上一章我们讲过所有的Magento模型都是继承自Mage_Core_Model_Abstract / Varien_Object.简单模型和EAV模型的区别在于资源模型(Model Resource).虽然所有的资源模型都最终继承"Mage_Core_Model_Resrouce_Abstract",但是简单模型是直接继承"Mage_

magento 开发 -- 深入理解Magento第四章 – 模型和ORM基础

第四章 – 模型和ORM基础 对于任何一个MVC架构,模型(Model)层的实现都是占据了很大一部分.对于Magento来说,模型占据了一个更加重要的位置,因为它常常包含了一部分商业逻辑代码(可以说它对,也可以说它错).这些代码在其他的MVC框架中往往出现在控制器或者帮助函数中. 传统的PHP MVC架构中的模型 本来MVC的定义就不是很清晰,不同的人有不同的看法,而对于模型的定义争议就更多了.在MVC模式被广泛采用之前,PHP程序员往往通过SQL语句直接操作数据库.也有些程序员通过一个SQL抽

magento 开发 -- 深入理解Magento第五章 – Magento资源配置

  第五章 – Magento资源配置 对于任何一个更新频繁的项目来说,保持开发环境和生产环境的数据库同步是件很头疼的事情.Magento提供了一套系统,用版本化的资源迁移脚本来解决这个问题. 上一章,我们为 Helloworld Blogpost 创建了一个模型.我们直接通过SQL语句"CREATE TABLE"来创建数据表.在这一章,我们将为Helloworld模块创建一个资源配置(Setup Resource)用于创建数据表.我们也会创建一个模块升级脚本,用来升级已经安装的模块.

android开发-新建一个android helloworld应用,但运行后只显示android几个字,不显示helloworld.

问题描述 新建一个android helloworld应用,但运行后只显示android几个字,不显示helloworld. 工程启动后,console显示以下报错信息 Android Launch! [2014-03-31 15:07:08 - firstapp] adb is running normally. [2014-03-31 15:07:08 - firstapp] Performing com.example.firstapp.MainActivity activity laun