久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Magento 中創建一個簡單的“Hello World"

How do I create a simple #39;Hello World#39; module in Magento?(如何在 Magento 中創建一個簡單的“Hello World模塊?)
本文介紹了如何在 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

  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

現在嘗試加載頁面.進步!你會得到一個 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.

這篇關于如何在 Magento 中創建一個簡單的“Hello World"模塊?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 免费在线观看黄网站 | 国产激情网站 | 欧美自拍第一页 | 国产精品爱久久久久久久 | 产真a观专区 | 欧美激情一区二区三级高清视频 | 一区二区在线 | 免费av观看 | 亚洲欧美日韩精品久久亚洲区 | 久干网| 亚洲欧美日韩在线 | 久久久久久免费毛片精品 | 一区二区三区免费在线观看 | 久久久久久亚洲精品 | 性视频网 | 久视频在线观看 | 国产精品一区二区三区四区 | 久久99精品国产 | 久久久免费观看视频 | 欧美日韩精品一区二区三区四区 | 最新国产视频 | 欧美日韩久久久久 | 国产999精品久久久久久 | 日日噜噜夜夜爽爽狠狠 | 中文字幕精品一区 | 亚洲综合二区 | 91精品综合久久久久久五月天 | 精品日韩| 91丨九色丨国产在线 | 精品国产高清一区二区三区 | 男人天堂久久久 | 国产精品精品视频一区二区三区 | 精品久久久久久国产 | 黄色网址在线免费观看 | 91精品国产91久久久久游泳池 | 美国a级毛片免费视频 | 国产一区二区免费 | 老牛嫩草一区二区三区av | 国产乱码精品一区二区三区中文 | 国产91综合一区在线观看 | 国产一区二区三区四区在线观看 |