如何将独立的 PHP 文件转换为 Magento 的 MVC

2021-12-19 00:00:00 php magento magento-1.4 magento-1.7

我有一项任务是将独立的 PHP 文件转换为 Magento 的 MVC.这些 PHP 文件是由另一位开发人员创建的.PHP 文件中的代码访问数据库,将结果转换为 JSONP 格式并转发给前端开发人员.

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 一无所知.这个转换任务和Magento文件夹中app/code/core/Mage中的模块类似吗??我怎样才能做到这一点?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?

我包含了需要转换为 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);
?>

那么我怎样才能把这个文件转换成 Magento 的 MVC 风格呢??是否需要将此文件转换为 magento MVC?

So how can I convert this file to Magento's MVC style?? IS this necessary to convert this file to magento MVC?

推荐答案

我认为他们要求你做的是转换看起来像这样的代码

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");
....

进入 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 的理解会出错

Name of new custom module - try to keep at least first letter capital, or there WILL BE truble with Magento's understanding

appcodelocalMyNamespaceAppnameBlock

在经典的MVC架构中,这代表MVC的View部分

In classic MVC architecture, this represents View part of MVC

appcodelocalMyNamespaceAppnamecontrollers

这相当容易理解,如果没有,玩得开心: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 架构中最重要的部分——将所有事物连接在一起的 xml 字段

Contains the most significant part in Magento's MVC architecture - the xml field that will connect all things together

appcodelocalMyNamespaceAppnameHelper

适用于包含可重复例程或简单程序方法的文件

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

了解它的用途很有趣,它用于定义自定义数据库表并处理对扩展程序的任何升级.

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/电子商务/magento/basic-folder-structure-for-new-magento-module/

相关文章