問題描述
我有一項(xiàng)任務(wù)是將獨(dú)立的 PHP 文件轉(zhuǎn)換為 Magento 的 MVC.這些 PHP 文件是由另一位開發(fā)人員創(chuàng)建的.PHP 文件中的代碼訪問數(shù)據(jù)庫,將結(jié)果轉(zhuǎn)換為 JSONP 格式并轉(zhuǎn)發(fā)給前端開發(fā)人員.
I have a task to convert the standalone PHP files to Magento's MVC. These PHP files were created by another developer. The code in the PHP file accesses the database, converts the result into JSONP format and forward it to the frontend developer.
我對 Magento 的 MVC 一無所知.這個(gè)轉(zhuǎn)換任務(wù)和Magento文件夾中app/code/core/Mage
中的模塊類似嗎??我怎樣才能做到這一點(diǎn)?magento MVC 和 PHP MVC 一樣嗎?
I don't have any knowledge of Magento's MVC. Is this task of conversion similar to the modules in the app/code/core/Mage
in the Magento folder?? How can I do this? Is the magento MVC the same as the PHP MVC?
我包含了需要轉(zhuǎn)換為 Magento MVC 的 php 文件.這樣你會更容易理解..
I am including the php file that I need to convert to Magento MVC. So it will be easier for you to understand..
<?php header('content-type: application/json; charset=utf-8');
$link = mysqli_connect("localhost", "db_username", "password", "db_dbname");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$pid = $_REQUEST['prodid'];
/* Select queries return a resultset */
$result = mysqli_query($link, "SELECT round(rating_summary / 20) AS search_rating FROM review_entity_summary where store_id = 1 and entity_pk_value=" . $pid);
// printf("Select returned %d rows.
" . "<br>
", mysqli_num_rows($result)) . "<br>
";
//$string = $_REQUEST['varname'];
$rows = array();
/* while ($row = $result->fetch_row()) {
printf("%s
", $row[0]);
}*/
//while($r = mysql_fetch_assoc($result)) {
while ($r = mysqli_fetch_assoc($result)) {
$rows = $r;
//print_r ($rows) . "<br>
";
}
$json_data = json_encode($rows);
print_r ($json_data);
/* free result set */
// mysqli_free_result($result)
mysqli_close($link);
?>
那么我怎樣才能把這個(gè)文件轉(zhuǎn)換成 Magento 的 MVC 風(fēng)格呢??是否需要將此文件轉(zhuǎn)換為 magento MVC?
So how can I convert this file to Magento's MVC style?? IS this necessary to convert this file to magento MVC?
推薦答案
我認(rèn)為他們要求你做的是轉(zhuǎn)換看起來像這樣的代碼
I think what they are asking you to do, is to convert code that look like
require_once 'path/to/magento'. "/Mage.php";
umask(0);
Mage::app("default");
....
進(jìn)入 Magento MVC(模塊)
In to Magento MVC (module)
appcodelocalMyNamespace
如果您是 OOP 的新手,請看這里:http://www.php.net/manual/en/language.namespaces.rationale.php
If you're new to OOP, take a look here: http://www.php.net/manual/en/language.namespaces.rationale.php
appcodelocalMyNamespaceAppname
新自定義模塊的名稱 - 盡量保持至少首字母大寫,否則 Magento 的理解會出錯(cuò)
Name of new custom module - try to keep at least first letter capital, or there WILL BE truble with Magento's understanding
appcodelocalMyNamespaceAppnameBlock
在經(jīng)典的MVC架構(gòu)中,這代表MVC的View部分
In classic MVC architecture, this represents View part of MVC
appcodelocalMyNamespaceAppnamecontrollers
這相當(dāng)容易理解,如果沒有,玩得開心:http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
This is fairly easy to understand, if not, have fun: http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
appcodelocalMyNamespaceAppname etc
包含 Magento MVC 架構(gòu)中最重要的部分——將所有事物連接在一起的 xml 字段
Contains the most significant part in Magento's MVC architecture - the xml field that will connect all things together
appcodelocalMyNamespaceAppnameHelper
適用于包含可重復(fù)例程或簡單程序方法的文件
Intended for files that contain repeatable routines or simple procedural methods
appcodelocalMyNamespaceAppnameModel
和控制器一樣,看上面的鏈接
Same thing as for controller, take a look at the link above
appcodelocalMyNamespaceAppnamesql
了解它的用途很有趣,它用于定義自定義數(shù)據(jù)庫表并處理對擴(kuò)展程序的任何升級.
This was interesting thing to find out what's it for, it's to define custom database tables and process any upgrades to your extension.
etcmodules
包含 Magento 中包含的所有模塊 - 這是我們模塊真正開始的地方
Contains all Modules included in Magento - here's where it all really begins for our module
參見 http://inchoo.net/電子商務(wù)/magento/basic-folder-structure-for-new-magento-module/
這篇關(guān)于如何將獨(dú)立的 PHP 文件轉(zhuǎn)換為 Magento 的 MVC的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!