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

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

问题描述

如何在Magento中完成以下操作?




  • 使用控制器/视图显示Hello World模型方法。所以,如果我去 http://example.com/myController 它会显示字符串'Hello World'。


  • 如何添加字符串(例如,标题,页脚等)这个控制器的一个方法(如果有必要,一个新的控制器),它与一个模型交互,并执行查询 Select * FROM articles where id = '10' row(包含 id,title,content )到控制器?然后使用控制器包含一个视图,它将显示此行。因此,转到 http://example.com/myController/show_row (或类似的东西)将在视图中显示该行。 (不需要花哨,只需 echo $ row-> id; 或类似的东西可以工作。)




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

解决方案

首先,我强烈建议您购买来自PHP Architect的PDF /电子书。它是US $ 20,但是是我能找到的唯一直接的这里如何Magento工作资源。我也已开始在我自己的网站上撰写 Magento教程



其次,如果您有选择,并且不是有经验的程序员,或者无法访问经验丰富的程序员(最好是PHP Java),另一个购物车。 Magento是精心设计的,但它被设计成一个购物车解决方案,其他程序员可以在其上构建模块。



第三,Magento MVC与 Ruby on Rails Django CodeIgniter CakePHP 等MVC模型,这些天流行的PHP开发人员。我认为它是基于 Zend 模型,整个事情是非常Java OOP的。您需要关心的两个控制器。模块/ frontName控制器,然后MVC控制器。



第四,Magento应用程序本身是使用相同的模块系统,你将使用,核心代码是一个有用的学习策略。此外,很多Magento的工作是覆盖现有的类。我在这里覆盖的是创建新功能,而不是覆盖。在你看代码示例时,请记住这一点。



我将从你的第一个问题开始,显示如何设置控制器/路由器响应特定的URL。这将是一个小小说。我可能有时间以后的模型/模板相关的主题,但现在,我不。



Magento使用 EAV 数据库架构。尽可能尝试使用系统提供的模型对象来获取所需的信息。我知道这是SQL表中的所有,但最好不要想到抓取数据使用原始SQL查询,否则你会疯了。



最后的免责声明。我一直在使用Magento大约两三个星期,所以需要注意。



创建模块


$ b这是一个练习, $ b

Magento的所有添加和定制都是通过模块完成的。所以,你需要做的第一件事是创建一个新的模块。在 app / modules 中创建一个名为以下的XML文件

  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 是您的模块的名称。



清除应用缓存



现在模块文件已经就位,我们需要让Magento知道它(并检查我们的工作)。在管理应用程式


  1. 前往系统 - >快取管理


  2. 点击保存缓存设置

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


  1. 前往System-> Configuration

  2. 按一下[进阶]

  3. 在禁用模块输出设置框中,查找名为MyCompanyName_HelloWorld的新模块

性能减慢,您可能想在开发/学习期间关闭应用程序缓存。



设置目录结构









$ b b $ b

接下来,我们需要为模块设置一个目录结构。

  mkdir -p app / code / local / MyCompanyName / HelloWorld / Block 
mkdir -p app / code / local / MyCompanyName / HelloWorld / controllers
mkdir -p app / code / local / MyCompanyName / HelloWorld /模型
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 

并在配置文件中添加以下内容,这本质上是一个空白配置。

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

过于简单的事情,这个配置文件会让你告诉Magento你想要运行什么代码。



设置路由器



接下来,我们需要设置模块的路由器。这将让系统知道我们正在以

的形式处理任何URL。

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

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

 < config& 
<! - ... - >
< frontend>
< routers>
<! - the< helloworld>标记名似乎是任意的,但由
约定应该匹配下面的frontName标记 - >
< helloworld>
< use> standard< / use>
< args>
< module> MyCompanyName_HelloWorld< / module>
< frontName> helloworld< / frontName>
< / args>
< / helloworld>
< / routers>
< / frontend>
<! - ... - >
< / config>

这里说的是任何带有helloworld的frontName的网址...

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



应使用frontName控制器MyCompanyName_HelloWorld。



配置到位,当你加载helloworld页面上面,你会得到一个404页面。这是因为我们没有为我们的控制器创建一个文件。现在让我们这样做。

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

现在尝试加载页面。进展!而不是404,你会得到一个PHP / Magento异常

 加载控制器文件,类不存在

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

 < ;?php 
类MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction(){
echo我们只是为了表明这是所谓的,通常你会有某种重定向在这里;
}
}
?>

我们刚刚设置的是module / frontName控制器。
这是默认控制器和模块的默认操作。
如果你想添加控制器或操作,你必须记住,Magento URL的树的第一部分是不可变的,他们总是这样的 http://example.com/magento/ index.php / frontName / controllerName / actionName



所以如果你想匹配这个url

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

你必须有一个FooController,你可以这样做:

  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';
}
}
?>

事后来了。
因此 http://example.com/magento/index.php/helloworld/foo 将匹配控制器FooController和操作indexAction,而不是操作fooAction IndexController。如果你想有一个fooAction,在控制器IndexController你必须调用这个控制器显式地这样:
http://example.com/magento/index.php/helloworld/ index / foo ,因为url的第二部分是且将始终是controllerName。
此行为是Magento中捆绑的Zend Framework的继承。



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

  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
code>

所以,这应该给你一个基本的想法如何Magento派遣到控制器。从这里我推荐在现有的Magento控制器类中查看如何使用模型和模板/布局系统。


How can the following be accomplish in Magento?

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

  • How to 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.)

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

解决方案

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.

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.

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.

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.

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 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.

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.

Create a module

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 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.

Clear the application cache

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. Go to System->Cache Management
  2. Select Refresh from the All Cache menu
  3. Click Save Cache settings

Now, we make sure that Magento knows about the module

  1. Go to System->Configuration
  2. Click Advanced
  3. In the "Disable modules output" setting box, look for your new module named "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.

Setup the directory structure

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

And add a configuration file

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>

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

Setting up the router

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>

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

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

should use the frontName controller MyCompanyName_HelloWorld".

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

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";
    }
}
?>

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

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';
    }
}
?>

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.

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

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天全站免登陆