magento开发一览

author:jim
date: 2016-6-13

文件目录结构

/app –程序根目录
/app/etc –全局配置文件目录
/app/code –所有模块安装其模型和控制器的目录
/app/code/core –核心代码或经过认证得模块,如果要升级不要这里的代码
/app/code/community –社区版的模块目录
/app/code/local –定制代码目录
/app/code/core/Mage –magento默认命名空间
/app/code/core/Mage/{Module} –模块根目录
/app/code/core/Mage/{Module}/etc –模块的配置文件目录
/app/code/core/Mage/{Module}/controllers –模块的控制器
/app/code/core/Mage/{Module}/Block –显示块的逻辑类
/app/code/core/Mage/{Module}/Model –模块的对象模型
/app/code/core/Mage/{Module}/Model/Mysql4 –模块的资源模型
/app/code/core/Mage/{Module}/sql –模块各个版本的安装和升级用sql 
/app/code/core/Mage/{Module}/sql/{resource}/ - 升级是需要的资源模型
/app/code/core/Mage/{Module}/sql/{resource}/{type}-{action}-{versions}.(sql|php) –资源升级文件例如: mysql4-upgrade-0.6.23-0.6.25.sql 
/app/design –设计包目录(layouts, templates, translations) 
/app/design/frontend –前端的设计
/app/design/adminhtml –后台管理设计
/app/design/{area}/{package}/{theme} –定制的主题
/app/design/{area}/{package}/{theme}/layout –定义显示块的 .xml 文件
/app/design/{area}/{package}/{theme}/template – .phtml (html with php tags)模版
/app/design/{area}/{package}/{theme}/locale –Zend_Translate 兼容的主题用的文字翻译
/app/locale –本地化文件
/app/locale/{locale (en_US)} –Zend_Translate 兼容的模块用的文字翻译
/skin/{area}/{package}/{theme}/- css和图像
/lib –公用库
/js – javascripts 
/media –上传文件存放目录
/tests –测试目录
/var –临时文件目录

清除缓存

在终端删除var下的cache文件夹

rm -rf 项目根目录/var/cache    

路由规则

根据类名查找文件路径

用过zend framework的人都知道,自动加载auto loader这个东西,它会跟你类名中的下滑线去你的目录中需要对应的类文件。记住一点,下滑线代表下一级别的文件夹,如果你的类名与你的文件目录名不一 致,那么Magento根本不会理睬你。
举例来说:

App_Catalog_Block_Breadcrumbs → /app/code/local/App/Catalog/Block/Breadcrumbs.php
App_Catalog_Block_Category_View → /app/code/local/App/Catalog/Block/Category/View.php

禁用模块

  • 后台禁用
    System > Configuration > Advanced > Disable modules output

  • 编辑app/etc/modules目录下的xml文件

<active>true</active>
<codePool>community</codePool>

将true置为false并刷新缓存   
* 在app/etc/local.xml中禁用所有的local模块    
        
        <disable_local_modules>false</disable_local_modules> 

##MVC架构
PHP 的 MVC 架构:

在典型的 MVC 模式中,应用程序的流程是这样的:

>1.主入口点:index.php 由整个应用程序的路由机制来决定。

>2.在此基础上的路由机制和请求的URL模式,应用程序将调用相应的控制器。

>3.然后控制器调用相应的视图。

>4.最后,查看文件收集模型文件中的数据和显示数据    
    

Magento 的 MVC架构:

>1.主入口点:index.php 在此整个应用程序将被初始化。

>2.相应的请求的URL在对应的控制器上会被调用。

>3.控制器定义了页面和加载布局文件的那些页面。

>4.布局文件告诉该块文件使用的控制器。

>5.阻止文件收集模型和助手文件中的数据,并把它传递给模板文件。

>6.模板文件接收数据,并渲染HTML。

##程序序列图
![](http://admclub.com/wp-content/uploads/2012/02/Magento的程序序列图.png)
##核心模块
##核心模块关系图
![](http://admclub.com/wp-content/uploads/2012/02/Magento核心模块关系图.png)
##重写block、controller、Model、Helper
参考:[https://www.magentonotes.com/overload_magento_controller.html](https://www.magentonotes.com/overload_magento_controller.html)


##创建数据库

1. 建立模块   
2. 在系统的配置文件中激活模块
3. 在模块的配置文件中
4. 创建模型

    ![](https://segmentfault.com/img/bVtitE)

5. 创建集合类
    ![](https://segmentfault.com/img/bVtPaN)    
       
##获取所有启用的支付模块

class Alwayly_Vendor_Model_Activpayment
{

public function getActivPaymentMethods()
{
   $payments = Mage::getSingleton('payment/config')->getActiveMethods();

   $methods = array(array('value'=>'', 'label'=>Mage::helper('adminhtml')->__('--Please Select--')));

   foreach ($payments as $paymentCode=>$paymentModel) {
        $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
        $methods[$paymentCode] = array(
            'label'   => $paymentTitle,
            'value' => $paymentCode,
        );
    }

    return $methods;

} 

}

##常用方法
- 获取session

$session = Mage::getSingleton('customer/session');

- Request对象

Mage::app()->getRequest()

- 调用Model对象

Mage::getModel('infinity/model');

- 获取当前时间

Mage::getModel('core/date')->date(); date("Y-m-d", Mage::getModel('core/date')->timestamp(time()));

- session,cookie设置

    - Model:

    Mage::getModel(‘core/cookie’); Mage::getModel(‘core/session’);

    - Set Method:

    Mage::getSingleton(‘core/cookie’)->set(‘name’,'value’); Mage::getSingleton(‘core/session’)->set(‘name’,'value’);

    - Get method:
    
    Mage::getSingleton(‘core/cookie’)->get(‘name’); Mage::getSingleton(‘core/session’)->get(‘name’);

- 输出配置文件

//header(‘Content-Type: text/xml’); header(‘Content-Type: text/plain’); echo $config = Mage::getConfig() ->loadModulesConfiguration(‘system.xml’) ->getNode() ->asXML(); exit;

- Get URL for a Magento Category

Mage::getModel('catalog/category')->load(17)->getUrl();

- build your URL with valid keys

Mage::helper("adminhtml")->getUrl("mymodule/adminhtml_mycontroller/myaction/",array("param1"=>1,"param2"=>2));

- create key values

Mage::getSingleton('adminhtml/url')->getSecretKey("adminhtml_mycontroller","myaction");

- disable security feature in the admin panel

admin panel -> System -> Configuration -> Admin section: “Add Secret key to Urls”.

- 后台模块跳转

Mage::app()->getResponse()->setRedirect(Mage::helper('adminhtml')->getUrl("adminhtml/promo_quote/index"));

- 产品属性操作

$product = Mage::getModel('catalog/product')->getCollection()->getFirstItem(); foreach($product->getAttributes() as $att) { $group_id   = $att->getData('attribute_group_id'); $group      = Mage::getModel('eav/entity_attribute_group')->load($group_id); var_dump($group); } $attrSetName = 'my_custom_attribute'; $attributeSetId = Mage::getModel('eav/entity_attribute_set') ->load($attrSetName, 'attribute_set_name') ->getAttributeSetId();

- get a drop down lists options for a mulit-select attribute

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id'); foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){ $attributeArray[$option['value']] = $option['label']; }

- 获取栏目图片

public function getImageUrl($category) { return Mage::getModel('catalog/category')->load($category->getId())->getImageUrl(); } public function getThumbnailUrl($category) { $image=Mage::getModel('catalog/category')->load($category->getId())->getThumbnail(); if ($image) { $url = Mage::getBaseUrl('media').'catalog/category/'.$image; } return $url; }

- 产品缩略图

$_thumb = Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(50, 50)->setWatermarkSize('30×10');

- 判断是否首页:$this->getIsHomePage()

Mage::getSingleton('cms/page')->getIdentifier() == 'home'  && Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms' )


- 获取configurable产品simple product

if($_product->getTypeId() == "configurable"): $ids = $_product->getTypeInstance()->getUsedProductIds(); foreach ($ids as $id) : $simpleproduct = Mage::getModel('catalog/product')->load($id); $simpleproduct->getName()." – ".(int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty(); $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);

- get the attributes of Configurable Product.

$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);

- 当前路径

$currentUrl = $this->helper('core/url')->getCurrentUrl();

- 通过资源配置方式创建目录

$installer = $this; $installer->startSetup(); // make directory for font cache try { $domPdfFontCacheDir = join(DS, array('lib', 'Symmetrics', 'dompdf', 'fonts')); $domPdfFontCacheDir = Mage::getBaseDir('var') . DS . $domPdfFontCacheDir; if (!file_exists($domPdfFontCacheDir)) { mkdir($domPdfFontCacheDir, 0777, true); } } catch(Exception $e) { throw new Exception( 'Directory ' . $domPdfFontCacheDir . ' is not writable or couldn\'t be ' . 'created. Please do it manually.' . $e->getMessage() ); } $installer->endSetup();xample"</aParams> <beforeText></beforeText> <afterText></afterText> </action> </reference>

- 在controllers 实现跳转

Mage::app()->getFrontController() ->getResponse() ->setRedirect('http://your-url.com');

- 获取当前站点货币符号

$storeId = (int) $this->getRequest()->getParam('store', 0); $store=Mage::app()->getStore($storeId); $currencyCode=$store->getBaseCurrency()->getCode() $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId); $attributeData = $attribute->getData(); $frontEndLabel = $attributeData['frontend_label']; $attributeOptions = $attribute->getSource()->getAllOptions();

- 获取产品属性集

$sets = Mage::getResourceModel('eav/entity_attribute_set_collection') ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId()) ->load() ->toOptionHash();

- magento使用sql

$resource = Mage::getSingleton('core/resource'); $read= $resource->getConnection('core_read'); $tempTable = $resource->getTableName('infinity_contacts'); $_storeid = Mage::app()->getStore()->getId(); $wheres = "`status`=1 AND ( FIND_IN_SET(0, `store_id`)>0 OR FIND_IN_SET($_storeid, `store_id`)>0 )"; $select = $read->select() ->from($tempTable, array('count(*) as num')) ->where($wheres);

- 设置meta信息

$template = Mage::getConfig()->getNode('global/page/layouts/'.Mage::getStoreConfig("featuredproducts/general/layout").'/template'); $this->loadLayout(); $this->getLayout()->getBlock('root')->setTemplate($template); $this->getLayout()->getBlock('head')->setTitle($this->__(Mage::getStoreConfig("featuredproducts/general/meta_title"))); $this->getLayout()->getBlock('head')->setDescription($this->__(Mage::getStoreConfig("featuredproducts/general/meta_description"))); $this->getLayout()->getBlock('head')->setKeywords($this->__(Mage::getStoreConfig("featuredproducts/general/meta_keywords"))); $this->renderLayout();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容

  • GitChat技术杂谈 前言 本文较长,为了节省你的阅读时间,在文前列写作思路如下: 什么是 webpack,它要...
    萧玄辞阅读 12,697评论 7 110
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,657评论 18 139
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,272评论 9 467
  • 写在开头 先说说为什么要写这篇文章, 最初的原因是组里的小朋友们看了webpack文档后, 表情都是这样的: (摘...
    Lefter阅读 5,289评论 4 31
  • 学习流程 参考文档:入门Webpack,看这篇就够了Webpack for React 一. 简单使用webpac...
    Jason_Zeng阅读 3,138评论 2 16