如何在Magento中创建一个简单的"Hello World"模块? [英] How do I create a simple 'Hello World' module in Magento?

查看:87
本文介绍了如何在Magento中创建一个简单的"Hello World"模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Magento中完成以下任务?

How can the following be accomplished in Magento?

  • 使用控制器/视图/模型方法显示"Hello World"消息.因此,如果我转到http://example.com/myController,它将显示字符串"Hello World".能够在我的网站模板中显示此字符串(例如,页眉,页脚等)将是一个好处.

  • Display a "Hello World" message using a controller/view/model approach. So, if I went to http://example.com/myController it would show the string 'Hello World'. Being able to show this string within the template of my website (for example, the header, footer, etc.) will be a bonus.

如何向该控制器(或必要时新控制器)添加方法,该方法与模型交互并执行查询Select * FROM articles where id='10'并将行(包含列id, title, content)返回到控制器?然后使用控制器包括一个视图,该视图将显示该行.因此,转到http://example.com/myController/show_row(或类似内容)将在视图中显示该行. (无需花哨,只需echo $row->id;或类似的名称即可.)

How do I add a method to this controller (or a new controller if necessary), which interacts with a model, and performs the query Select * FROM articles where id='10' and returns the row (containing the columns id, title, content) to the controller? And then use the controller to include a view, which would display this row. So going to http://example.com/myController/show_row (or something similar) would display the row within a view. (No need to be fancy, just a echo $row->id; or something similar would work.)

关于Magento代码结构的任何其他信息也将非常有帮助.

Any other information about Magento's code structure will also be very helpful.

推荐答案

首先,我强烈建议您购买 Magento教程.

First and foremost, I highly recommend you buy the PDF/E-Book from PHP Architect. It's US$20, but is the only straightforward "Here's how Magento works" resource I've been able to find. I've also started writing Magento tutorials at my own website.

第二,如果您有选择,并且不是经验丰富的程序员或没有经验的程序员(最好是在PHP Java中使用),请选择另一个购物车. Magento经过精心设计,但被设计为购物车解决方案,其他程序员可以在其上构建模块.并不是为了让聪明的人(而不是程序员)容易理解它.

Second, if you have a choice, and aren't an experienced programmer or don't have access to an experienced programmer (ideally in PHP and Java), pick another cart. Magento is well engineered, but it was engineered to be a shopping cart solution that other programmers can build modules on top of. It was not engineered to be easily understood by people who are smart, but aren't programmers.

第三,Magento MVC与 Ruby on Rails CakePHP 等.流行的MVC模型这些天与PHP开发人员一起.我认为它基于 Zend 模型,整个过程非常类似于Java OOP.您需要关注两个控制器.模块/frontName控制器,然后是MVC控制器.

Third, Magento MVC is very different from the Ruby on Rails, Django, CodeIgniter, CakePHP, etc. MVC model that's popular with PHP developers these days. I think it's based on the Zend model, and the whole thing is very Java OOP-like. There's two controllers you need to be concerned about. The module/frontName controller, and then the MVC controller.

第四,Magento应用程序本身是使用与您将使用的模块系统一起构建的,因此在核心代码中四处寻找是一种有用的学习策略.另外,您要使用Magento进行的很多操作是覆盖现有的类.我在这里介绍的是创建新功能,而不是覆盖.当您查看那里的代码示例时,请记住这一点.

Fourth, the Magento application itself is built using the same module system you'll be using, so poking around the core code is a useful learning tactic. Also, a lot of what you'll be doing with Magento is overriding existing classes. What I'm covering here is creating new functionality, not overriding. Keep this in mind when you're looking at the code samples out there.

我将从您的第一个问题开始,向您展示如何设置控制器/路由器以响应特定的URL.这将是一部小小说.稍后我可能会花一些时间来讨论与模型/模板相关的主题,但到目前为止,我还没有.但是,我将简要地谈谈您的SQL问题.

I'm going to start with your first question, showing you how to setup a controller/router to respond to a specific URL. This will be a small novel. I might have time later for the model/template related topics, but for now, I don't. I will, however, briefly speak to your SQL question.

Magento使用 EAV 数据库体系结构.尽可能尝试使用系统提供的模型对象来获取所需的信息.我知道SQL表中都有它们,但是最好不要考虑使用原始SQL查询来获取数据,否则您会发疯的.

Magento uses an EAV database architecture. Whenever possible, try to use the model objects the system provides to get the information you need. I know it's all there in the SQL tables, but it's best not to think of grabbing data using raw SQL queries, or you'll go mad.

最终免责声明.我已经使用Magento大约两三个星期了,所以请注意.这是一种锻炼,目的是要尽我所能,同时也要帮助Stack Overflow.

Final disclaimer. I've been using Magento for about two or three weeks, so caveat emptor. This is an exercise to get this straight in my head as much as it is to help Stack Overflow.

对Magento的所有添加和自定义都是通过模块完成的.因此,您需要做的第一件事就是创建一个新模块.在app/modules中创建一个XML文件,命名如下

All additions and customizations to Magento are done through modules. So, the first thing you'll need to do is create a new module. Create an XML file in app/modules named as follows

cd /path/to/store/app
touch etc/modules/MyCompanyName_HelloWorld.xml

<?xml version="1.0"?>
<config>
     <modules>
        <MyCompanyName_HelloWorld>
            <active>true</active>
            <codePool>local</codePool>
        </MyCompanyName_HelloWorld>
     </modules>
</config>

MyCompanyName是用于修改的唯一名称空间,它不一定是您公司的名称,但是建议的约定是我的magento. HelloWorld是模块的名称.

MyCompanyName is a unique namespace for your modifications, it doesn't have to be your company's name, but that the recommended convention my magento. HelloWorld is the name of your module.

现在模块文件已经存在,我们需要让Magento知道它(并检查我们的工作).在管理应用程序中

Now that the module file is in place, we'll need to let Magento know about it (and check our work). In the admin application

  1. 转到系统->缓存管理
  2. 从所有缓存"菜单中选择刷新"
  3. 点击保存缓存设置"

现在,我们确保Magento知道该模块

Now, we make sure that Magento knows about the module

  1. 转到系统->配置
  2. 点击高级
  3. 在禁用模块输出"设置框中,查找名为"MyCompanyName_HelloWorld"的新模块.

如果您可以忍受性能下降的情况,则可能需要在开发/学习时关闭应用程序缓存.没有什么比沮丧的事还要忘记清除缓存并想知道为什么您的更改没有显示出来了.

If you can live with the performance slow down, you might want to turn off the application cache while developing/learning. Nothing is more frustrating then forgetting the clear out the cache and wondering why your changes aren't showing up.

接下来,我们需要为模块设置目录结构.您不需要所有这些目录,但是现在将它们全部设置都没有害处.

Next, we'll need to setup a directory structure for the module. You won't need all these directories, but there's no harm in setting them all up now.

mkdir -p app/code/local/MyCompanyName/HelloWorld/Block
mkdir -p app/code/local/MyCompanyName/HelloWorld/controllers
mkdir -p app/code/local/MyCompanyName/HelloWorld/Model
mkdir -p app/code/local/MyCompanyName/HelloWorld/Helper
mkdir -p app/code/local/MyCompanyName/HelloWorld/etc
mkdir -p app/code/local/MyCompanyName/HelloWorld/sql

并添加配置文件

touch app/code/local/MyCompanyName/HelloWorld/etc/config.xml

在配置文件中,添加以下内容,该文件本质上是空白"配置.

and inside the configuration file, add the following, which is essentially a "blank" configuration.

<?xml version="1.0"?>
<config>
    <modules>
        <MyCompanyName_HelloWorld>
            <version>0.1.0</version>
        </MyCompanyName_HelloWorld>
    </modules>
</config>

过于简化,此配置文件将使您告诉Magento您要运行什么代码.

Oversimplifying things, this configuration file will let you tell Magento what code you want to run.

接下来,我们需要设置模块的路由器.这将使系统知道我们正在处理任何形式为

Next, we need to setup the module's routers. This will let the system know that we're handling any URLs in the form of

http://example.com/magento/index.php/helloworld

因此,在您的配置文件中,添加以下部分.

So, in your configuration file, add the following section.

<config>
<!-- ... -->
    <frontend>
        <routers>
            <!-- the <helloworld> tagname appears to be arbitrary, but by
            convention is should match the frontName tag below-->
            <helloworld>
                <use>standard</use>
                <args>
                    <module>MyCompanyName_HelloWorld</module>
                    <frontName>helloworld</frontName>
                </args>
            </helloworld>
        </routers>
    </frontend>
<!-- ... -->
</config>

您在这里所说的是带有helloworld的frontName的任何URL ...

What you're saying here is "any URL with the frontName of helloworld ...

http://example.com/magento/index.php/helloworld

应使用frontName控制器MyCompanyName_HelloWorld".

should use the frontName controller MyCompanyName_HelloWorld".

因此,使用上面的配置,当您加载上面的helloworld页面时,您将获得404页面.那是因为我们还没有为控制器创建文件.现在开始吧.

So, with the above configuration in place, when you load the helloworld page above, you'll get a 404 page. That's because we haven't created a file for our controller. Let's do that now.

touch app/code/local/MyCompanyName/HelloWorld/controllers/IndexController.php

现在尝试加载页面.进步!而不是404,您将获得一个PHP/Magento异常

Now try loading the page. Progress! Instead of a 404, you'll get a PHP/Magento exception

Controller file was loaded but class does not exist

因此,打开我们刚刚创建的文件,然后粘贴以下代码.该类的名称必须基于您在路由器中提供的名称.

So, open the file we just created, and paste in the following code. The name of the class needs to be based on the name you provided in your router.

<?php
class MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        echo "We're echoing just to show that this is what's called, normally you'd have some kind of redirect going on here";
    }
}

我们刚刚设置的是module/frontName控制器. 这是模块的默认控制器和默认操作. 如果要添加控制器或动作,则必须记住,Magento URL的树的第一部分是不可变的,它们将始终以这种方式进行http://example.com/magento/index.php/frontName/controllerName/actionName

What we've just setup is the module/frontName controller. This is the default controller and the default action of the module. If you want to add controllers or actions, you have to remember that the tree first part of a Magento URL are immutable they will always go this way http://example.com/magento/index.php/frontName/controllerName/actionName

因此,如果您想匹配此网址

So if you want to match this url

http://example.com/magento/index.php/helloworld/foo

您将必须具有一个FooController,您可以通过以下方式进行操作:

You will have to have a FooController, which you can do this way :

touch app/code/local/MyCompanyName/HelloWorld/controllers/FooController.php

<?php
class MyCompanyName_HelloWorld_FooController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        echo 'Foo Index Action';
    }

    public function addAction(){
        echo 'Foo add Action';
    }

    public function deleteAction(){
        echo 'Foo delete Action';
    }
}

请注意,默认控制器IndexController和默认操作indexAction可以是隐式的,但如果后面有某些内容,则必须是显式的. 因此,http://example.com/magento/index.php/helloworld/foo将与控制器FooController和动作indexAction匹配,而不与IndexController的动作fooAction匹配.如果要使用fooAction,则必须在控制器IndexController中像下面这样显式调用此控制器: http://example.com/magento/index.php/helloworld/index/foo,因为URL的第二部分是且将始终是controllerName. 此行为是Magento中捆绑的Zend Framework的继承.

Please note that the default controller IndexController and the default action indexAction can by implicit but have to be explicit if something come after it. So http://example.com/magento/index.php/helloworld/foo will match the controller FooController and the action indexAction and NOT the action fooAction of the IndexController. If you want to have a fooAction, in the controller IndexController you then have to call this controller explicitly like this way : http://example.com/magento/index.php/helloworld/index/foo because the second part of the url is and will always be the controllerName. This behaviour is an inheritance of the Zend Framework bundled in Magento.

您现在应该可以访问以下URL,并查看echo语句的结果

You should now be able to hit the following URLs and see the results of your echo statements

http://example.com/magento/index.php/helloworld/foo
http://example.com/magento/index.php/helloworld/foo/add
http://example.com/magento/index.php/helloworld/foo/delete

因此,这应该给您有关Magento如何分派给控制器的基本思想.从这里开始,我建议戳一下现有的Magento控制器类,以了解应如何使用模型和模板/布局系统.

So, that should give you a basic idea on how Magento dispatches to a controller. From here I'd recommended poking at the existing Magento controller classes to see how models and the template/layout system should be used.

这篇关于如何在Magento中创建一个简单的"Hello World"模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆