网站公司打电话来说做网站,南宁做网站推广的公司哪家好,网站建设价目表,加入google广告wordpressM2不是一个标准的MVC架构。 这是m2的app/code/Magento/Catalog插件代码。 可以看到#xff0c;它有Controller,也有Model,也有view. 奇怪的是,在Controller找不到调用模版的代码。 这是因为我们之前讲过,m2的页面都是用xml写的#xff0c;xml里是由若干个block组成的。block里…M2不是一个标准的MVC架构。 这是m2的app/code/Magento/Catalog插件代码。 可以看到它有Controller,也有Model,也有view. 奇怪的是,在Controller找不到调用模版的代码。 这是因为我们之前讲过,m2的页面都是用xml写的xml里是由若干个block组成的。block里调用的template就是view下面的template。
所以这个Controller加载的是该页面的xml文件。xml再解析输出成html。
细心的你会发现,这个插件里也有一个Block目录没错就是它。 xml里的就是这个block这个block里才是真正的逻辑功能代码。 到处都是block。
M2有自己的语法封装了很多类。
增删改查
一个标准的php类如下:
?php
namespace Zou\Test\Block;
class Demo extends \Magento\Framework\View\Element\Template{protected $_storeManager;protected $_scopeConfig;protected $_productFactory;protected $_productCollectionFactory;protected $_categoryFactory;protected $_categoryCollectionFactory;protected $_customerFactory;protected $_customerCollectionFactory;protected $_orderFactory;protected $_orderCollectionFactory;public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,\Magento\Store\Model\StoreManagerInterface $storeManager,\Magento\Catalog\Model\ProductFactory $productFactory,\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,\Magento\Catalog\Model\CategoryFactory $categoryFactory,\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,\Magento\Customer\Model\CustomerFactory $customerFactory,\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory,\Magento\Sales\Model\OrderFactory $orderFactory,\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory) {$this-_scopeConfig $scopeConfig;$this-_storeManager $storeManager;$this-_productFactory $productFactory;$this-_productCollectionFactory $productCollectionFactory;$this-_categoryFactory $categoryFactory;$this-_categoryCollectionFactory $categoryCollectionFactory;$this-_customerFactory $customerFactory;$this-_customerCollectionFactory $customerCollectionFactory;}//通过产品id获取产品的name(属性)public function getProductName($pid1){$product $this-_productFactory-create()-load($pid);return $product-getName();}//获取价格大于100的产品public function getProductsByPrice($price100){$productCollection $this-_productCollectionFactory-create();$productCollection-addAttributeToSelect(price);$productCollection-addAttributeToFilter(price, array(gt$price));foreach ($productCollection as $product) {echo $product-getPrice();}return $productCollection;}//把id为10的产品价格修改为50public function setProduct(){$price 50;$pid 10;$product $this-_productFactory-create()-load($pid);$product-setPrice(50);$product-save();}//删掉id为1的产品public function deleteProduct($pid1){$product $this-_productFactory-create()-load($pid);$product-delete();}}
?namespace对phper应该不陌生了现在php7新框架基本上都是用的命名空间。
在__construct里的声明的需要的类。
比如
\Magento\Catalog\Model\ProductFactory是产品模型类通过这个类你可以得到单个产品的任何信息(比如属性)。\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory是产品数据集通过这个类你可以任意按条件(比如属性)搜索过滤产品\Magento\Catalog\Model\CategoryFactory是分类模型类通过这个类你可以得到单个分类的任何信息(比如属性)。\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory是分类数据集,通过这个类你可以任意按条件(比如属性)搜索过滤分类\Magento\Customer\Model\CustomerFactory $customerFactory是客户联系人模型类通过这个类你可以得到单个Customer的任何信息(比如属性)。\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory是联系人数据集,通过这个类你可以任意按条件(比如属性)搜索过滤联系人\Magento\Sales\Model\OrderFactory是订单模型类通过这个类你可以得到单个订单的任何信息(比如属性)。\Magento\Sales\Model\ResourceModel\Order\CollectionFactory是订单数据集,通过这个类你可以任意按条件(比如属性)搜索过滤订单
通过上面这个简易的php代码你就学会了增删改查是不是非常简单粗暴
通过M2提供的模型数据资源类,就可以从容优雅的进行增删改查。
先卖个关子,具体的我们在第四章做插件的时候 会细讲。