如何在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 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代码结构的信息也将非常有帮助。


I've been wrestling with Magento for the last month or so and I'm still trying to figure it out. So this is a case of the blind leading the blind. There's little in the way of documentation and the forum/wiki is chaotic at best. Not only that, but there are several solutions that are either outdated or far from optimal. I'm not sure if you have a project or just trying to figure it out, but it's probably easier if you started with modifying existing functionality as opposed to creating something completely new. For that I'd definately go with the "Recommended articles for developers" in the wiki. The new payment method one was a real eye-opener.

对于调试,我绝对推荐使用FirePHP,并在出现问题时查看HTML源代码。ole echo debug方法并不能很好地工作。

它的总体架构是如此复杂,以至于即使我完全理解它,我也需要写一本书来介绍它。我所能做的就是给你一些建议,我希望在我刚开始工作时有人能给我一些建议……

远离核心文件。不要修改它们,而是编写自己的模块并覆盖您需要的内容。

Magento使用由XML组成的配置文件来决定它需要做什么。为了让它运行你自己的东西,而不是核心功能,你需要正确的xml。不幸的是,没有关于如何构建XML的指南;您需要查看示例并进行一些严格的测试。更复杂的是,这些文件的内容很大程度上是区分大小写的。然而,如果你掌握了这些,你可以覆盖任何部分的基本功能,这使得一个非常强大的系统。

Magento使用Mage::getModel('mymodel')、Mage::getSingleton('mysingleton')、Mage::helper('myhelper')等方法返回特定类的对象。默认情况下,它在其核心名称空间中找到这些。如果你想让它使用你自己的,你需要在config.xml文件中重写这些。

类的名称必须与它们所在的文件夹相对应。

Magento中的许多对象最终扩展了一个叫做Varien_Object的东西。这是一个通用类(有点像瑞士军刀),它的用途是允许您动态地定义自己的方法/变量。例如,您将看到它被用作一个美化数组,将数据从一个方法传递到另一个方法。

在开发过程中,确保您的缓存是禁用的。它会让magento变得极其缓慢,但它会让你的头部免受很多创伤(因为它会撞到你的桌子上)。

你会看到$this经常被使用。它意味着一个不同的类,这取决于你看到的是什么文件。get_class($this)是您的朋友,特别是与FirePHP结合使用时。

把事情记在纸上。很多。有无数的小事实,你需要在遇到它们后的1-2天。

Magento喜欢OO。如果跟踪一个方法需要5-10个不同的类,不要感到惊讶。

点击这里阅读设计师指南。它主要是为图形设计师设计的,但你需要它来理解模块的输出将在哪里以及为什么结束。为此,不要忘记在管理面板的开发人员部分打开“模板路径提示”。

还有更多,但在这变成论文之前,我就讲到这里。

首先,我强烈建议您从PHP Architect购买PDF/电子书。这款游戏售价20美元,但却是我所能找到的唯一直接的“Magento是如何运作的”资源。我也开始在自己的网站上写Magento教程。

其次,如果您可以选择,并且不是一个有经验的程序员,或者没有机会接触到有经验的程序员(最好是PHP和Java程序员),请选择另一个购物车。Magento设计得很好,但它被设计成一个购物车解决方案,其他程序员可以在其上构建模块。它不是为了让那些聪明但不是程序员的人容易理解而设计的。

第三,Magento MVC与Ruby on Rails、Django、CodeIgniter、CakePHP等非常不同。MVC模型,这在PHP开发人员中很流行。我认为它是基于Zend模型的,而且整个过程非常像Java oop。有两个控制器需要注意。模块/frontName控制器,然后是MVC控制器。

第四,Magento应用程序本身是使用您将使用的相同模块系统构建的,因此研究核心代码是一种有用的学习策略。此外,使用Magento要做的很多事情都是重写现有的类。我在这里讨论的是创建新功能,而不是重写。在查看代码示例时,请记住这一点。

我将从你的第一个问题开始,向你展示如何设置一个控制器/路由器来响应特定的URL。这将是一部小小说。我以后可能会有时间讨论模型/模板相关的主题,但是现在,我没有时间。不过,我将简要地谈谈您的SQL问题。

Magento使用EAV数据库架构。只要有可能,尽量使用系统提供的模型对象来获取所需的信息。我知道这些都在SQL表中,但是最好不要使用原始SQL查询来获取数据,否则您会发疯的。

最后的免责声明。我已经用Magento两三个星期了,买者自负。这是一个练习,以得到这个在我的头脑中,因为它是帮助堆栈溢出。

创建模块

所有对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知道它(并检查我们的工作)。在管理应用程序中

进入“System->Cache Management” 从所有缓存菜单中选择刷新 单击“保存缓存设置”

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

进入“System->Configuration” 单击高级 在“禁用模块输出”设置框中,寻找名为“MyCompanyName_HelloWorld”的新模块

如果您可以接受性能下降,那么您可能希望在开发/学习时关闭应用程序缓存。没有什么比忘记清除缓存并想知道为什么您的更改没有显示更令人沮丧的了。

设置目录结构

接下来,我们需要为模块设置一个目录结构。您不需要所有这些目录,但是现在将它们全部设置起来也无妨。

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

在配置文件中,添加以下内容,这实际上是一个“空白”配置。

<?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> 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…

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

应该使用frontName控制器MyCompanyName_HelloWorld”。

因此,在完成上述配置之后,当您加载上面的helloworld页面时,将会看到一个404页面。这是因为我们还没有为控制器创建文件。我们现在就开始吧。

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

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

Controller file was loaded but class does not exist

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

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

如果你想匹配这个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';
    }
}

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语句的结果

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控制器类,看看应该如何使用模型和模板/布局系统。

我宁愿推荐Mage2Gen,这将帮助您生成样板,您可以只关注核心业务逻辑。这有助于加快速度。

A Magento Module is a group of directories containing blocks, controllers, helpers, and models that are needed to create a specific store feature. It is the unit of customization in the Magento platform. Magento Modules can be created to perform multiple functions with supporting logic to influence user experience and storefront appearance. It has a life cycle that allows them to be installed, deleted, or disabled. From the perspective of both merchants and extension developers, modules are the central unit of the Magento platform.

模块声明

我们必须使用配置文件来声明模块。作为Magento 2在模块的etc目录中搜索配置模块。现在我们将创建配置文件module.xml。

代码如下所示:

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Cloudways_Mymodule" setup_version="1.0.0"></module> </config>

模块注册 该模块必须使用Magento Component Registrar类在Magento 2系统中注册。现在我们将在模块根目录下创建registration.php文件:

app/code/Cloudways/Mymodule/registration.php

代码如下所示:

?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Cloudways_Mymodule',
__DIR__
);

检查模块状态 在执行以上步骤之后,我们将创建一个简单的模块。现在我们要检查模块的状态,使用下面的命令行检查它是启用还是禁用:

php bin/magento module:status

php bin/magento module:enable Cloudways_Mymodule

一旦你完成了整个过程,分享你的反馈

I was trying to make my module from magaplaza hello world tutorial, but something went wrong. I imported code of this module https://github.com/astorm/magento2-hello-world from github and it worked. from that module, i created it a categories subcategories ajax select drop downs Module. After installing it in aap/code directory of your magento2 installation follow this URL.. http://www.example.com/hello_mvvm/hello/world You can download its code from here https://github.com/sanaullahAhmad/Magento2_cat_subcat_ajax_select_dropdowns and place it in your aap/code folder. than run these commands...

php bin/magento setup:update
php bin/magento setup:static-content:deploy -f
php bin/magento c:c

现在,您可以使用以下URL http://{{www.example.com}}/hello_mvvm/hello/world检查模块功能