如何在 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 文章 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.
推荐答案
首先,我强烈建议您购买 PHP 架构师提供的 PDF/电子书.它是 20 美元,但它是我能找到的唯一简单的Magento 的工作原理"资源.我也开始在自己的网站上编写 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 有很大不同,Django, CodeIgniter、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 大约两三个星期了,所以请注意空客.这是一个练习,可以让我清楚地了解这一点,同时也可以帮助 StackOverflow.
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
- 进入系统->缓存管理
- 从所有缓存"菜单中选择刷新"
- 点击保存缓存设置
现在,我们确保 Magento 知道该模块
Now, we make sure that Magento knows about the module
- 进入系统->配置
- 点击高级
- 在禁用模块输出"设置框中,查找名为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
现在尝试加载页面.进步!你会得到一个 PHP/Magento 异常,而不是 404
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";
}
}
我们刚刚设置的是模块/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
所以如果你想匹配这个url
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 框架的继承.
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.
相关文章