文章列表


magento2中的EAV 和扩展属性以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中,EAV(Entity-Attribute-Value)模型是用于处理可扩展属性(例如商品、类别、顾客等)的标准方法。EAV 模型允许为每个实体创建任意数量的自定义属性。</p><p><br/></p><p>以下是一个简单的示例,演示如何在Magento 2中创建和使用自定义属性:</p><p><br/></p><p>创建自定义属性</p><p>要创建自定义属性,您需要在您的模块的 Setup/InstallData.php 文件中添加以下内容:</p><p><br/></p><pre class="brush:as3;toolbar:false">&lt;?php namespace&nbsp;My\Module\Setup; use&nbsp;Magento\Eav\Setup\EavSetupFactory; use&nbsp;Magento\Framework\Setup\InstallDataInterface; use&nbsp;Magento\Framework\Setup\ModuleContextInterface; use&nbsp;Magento\Framework\Setup\ModuleDataSetupInterface; class&nbsp;InstallData&nbsp;implements&nbsp;InstallDataInterface { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$eavSetupFactory; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct(EavSetupFactory&nbsp;$eavSetupFactory) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;eavSetupFactory&nbsp;=&nbsp;$eavSetupFactory; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;install(ModuleDataSetupInterface&nbsp;$setup,&nbsp;ModuleContextInterface&nbsp;$context) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eavSetup&nbsp;=&nbsp;$this-&gt;eavSetupFactory-&gt;create([&#39;setup&#39;&nbsp;=&gt;&nbsp;$setup]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eavSetup-&gt;addAttribute( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Catalog\Model\Product::ENTITY, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;my_attribute&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;type&#39;&nbsp;=&gt;&nbsp;&#39;text&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;label&#39;&nbsp;=&gt;&nbsp;&#39;My&nbsp;Attribute&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;input&#39;&nbsp;=&gt;&nbsp;&#39;textarea&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;required&#39;&nbsp;=&gt;&nbsp;false, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;visible_on_front&#39;&nbsp;=&gt;&nbsp;true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;global&#39;&nbsp;=&gt;&nbsp;\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>上面的示例创建了一个名为 my_attribute 的自定义属性,它将被添加到 Magento\Catalog\Model\Product 类中。</p><p><br/></p><p>使用自定义属性</p><p>要使用自定义属性,您可以使用以下代码示例:</p><p><br/></p><p></p><pre class="brush:as3;toolbar:false">$product&nbsp;=&nbsp;$objectManager-&gt;create(\Magento\Catalog\Model\Product::class); $product-&gt;setName(&#39;Product&nbsp;Name&#39;); $product-&gt;setData(&#39;my_attribute&#39;,&nbsp;&#39;My&nbsp;Attribute&nbsp;Value&#39;); $product-&gt;save();</pre><p><span style="color: #6a9955;"></span><br/></p><p>上面的示例创建了一个新产品,并设置了 name 和 my_attribute 属性的值。</p><p><br/></p><p>扩展自定义属性</p><p>如果您想添加更多选项或逻辑来控制自定义属性的行为,您可以通过创建插件来扩展自定义属性。以下是一个简单的示例:</p><p><br/></p><pre class="brush:as3;toolbar:false">&lt;?php namespace&nbsp;My\Module\Plugin; class&nbsp;ProductPlugin { &nbsp;&nbsp;&nbsp;&nbsp;/** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;\Magento\Catalog\Model\Product&nbsp;$subject &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;string&nbsp;$result &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;afterGetMyAttribute(\Magento\Catalog\Model\Product&nbsp;$subject,&nbsp;$result) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Add&nbsp;custom&nbsp;logic&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$result; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>上面的示例创建了一个名为 ProductPlugin 的插件,它使用 after 方法修改 Magento\Catalog\Model\Product 类中的 getMyAttribute 方法。</p><p><br/></p><p>请注意,上面的示例仅适用于演示目的。在实际开发中,请避免使用 $objectManager,而是使用依赖注入和接口实现。</p><p><br/></p>

magento2中的代码生成以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在 Magento 2 中,您可以使用代码生成器(Code Generator)来生成基本代码结构,包括模块、控制器、块、模型、数据访问对象(Data Access Objects)等。以下是在 Magento 2 中使用代码生成器生成一个简单模块的一般步骤:</p><p>打开终端并进入 Magento 2 的根目录。</p><p>运行以下命令,使用代码生成器创建一个新模块:</p><pre>php&nbsp;bin/magento&nbsp;module:generate&nbsp;--namespace=Vendor&nbsp;--module-name=Example</pre><p>在上述命令中,--namespace 参数表示您的模块的命名空间,--module-name 参数表示您的模块的名称。例如,如果您要创建一个名为 Example 的模块,其命名空间为 Vendor,则可以使用以下命令:</p><pre>php&nbsp;bin/magento&nbsp;module:generate&nbsp;--namespace=Vendor&nbsp;--module-name=Example</pre><p>根据需要使用代码生成器创建其他代码文件,例如控制器、块、模型等。以下是创建控制器的示例命令:</p><pre>php&nbsp;bin/magento&nbsp;generate:controller&nbsp;--route=example&nbsp;--controller-name=Index&nbsp;--acl-resource=Vendor_Example::example</pre><p>在上述命令中,--route 参数表示您的控制器的路由,--controller-name 参数表示您的控制器的名称,--acl-resource 参数表示您的控制器的 ACL 资源名称。</p><p>在上述示例命令中,我们创建了一个名为 Index 的控制器,其路由为 example,ACL 资源名称为 Vendor_Example::example。控制器类将自动创建在 Vendor/Example/Controller/Index.php 文件中。</p><p>运行以下命令,以确保 Magento 2 能够识别您的新模块:</p><pre>php&nbsp;bin/magento&nbsp;setup:upgrade</pre><p>以上是在 Magento 2 中使用代码生成器创建一个简单模块的一般步骤。请注意,生成器生成的代码只提供了基本的代码结构,您需要根据实际需要对代码进行修改和扩展。</p>

magento2中的代理以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中,您可以使用代理来访问远程服务或创建一些虚拟对象。代理是通过PHP的魔术方法来实现的,可以在不明确实例化对象的情况下访问方法和属性。</p><p><br/></p><p>以下是一个使用代理的代码示例:</p><p><br/></p><pre class="brush:as3;toolbar:false">&lt;?php namespace&nbsp;MyVendor\MyModule\Model; use&nbsp;Magento\Framework\App\Config\ScopeConfigInterface; use&nbsp;Magento\Framework\ObjectManagerInterface; class&nbsp;MyModel { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$objectManager; &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$scopeConfig; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct(ObjectManagerInterface&nbsp;$objectManager,&nbsp;ScopeConfigInterface&nbsp;$scopeConfig) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;objectManager&nbsp;=&nbsp;$objectManager; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;scopeConfig&nbsp;=&nbsp;$scopeConfig; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getSomeData() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$remoteService&nbsp;=&nbsp;$this-&gt;objectManager-&gt;create(RemoteServiceInterface::class); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data&nbsp;=&nbsp;$remoteService-&gt;getData(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$virtualObject&nbsp;=&nbsp;$this-&gt;objectManager-&gt;create(VirtualObject::class); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result&nbsp;=&nbsp;$virtualObject-&gt;doSomething($data); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$result; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__call($method,&nbsp;$args) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$proxy&nbsp;=&nbsp;$this-&gt;objectManager-&gt;get(MyModelProxy::class); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$proxy-&gt;$method(...$args); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__get($property) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$proxy&nbsp;=&nbsp;$this-&gt;objectManager-&gt;get(MyModelProxy::class); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$proxy-&gt;$property; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__set($property,&nbsp;$value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$proxy&nbsp;=&nbsp;$this-&gt;objectManager-&gt;get(MyModelProxy::class); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$proxy-&gt;$property&nbsp;=&nbsp;$value; &nbsp;&nbsp;&nbsp;&nbsp;} } class&nbsp;MyModelProxy&nbsp;extends&nbsp;MyModel { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct(ObjectManagerInterface&nbsp;$objectManager,&nbsp;ScopeConfigInterface&nbsp;$scopeConfig) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::__construct($objectManager,&nbsp;$scopeConfig); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;someMethod() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Your&nbsp;custom&nbsp;code&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getSomeProperty() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Your&nbsp;custom&nbsp;code&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;setSomeProperty($value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Your&nbsp;custom&nbsp;code&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>在上面的示例中,MyModel 类使用代理来访问远程服务和创建虚拟对象。MyModel 类中的__call、__get和__set方法将被代理类 MyModelProxy 使用,并在需要时添加自定义逻辑。</p><p><br/></p><p>请注意,代理应谨慎使用,因为它可能会降低您的应用程序性能。在使用代理时,请始终考虑最佳实践和性能。</p><p><br/></p>

magento2中的工厂以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中,工厂类允许您在没有直接实例化对象的情况下创建对象。 工厂类是一个生成其他类的对象的类,它是 Magento 2 的核心概念之一。</p><p><br/></p><p>以下是一个使用工厂类的代码示例:</p><p><br/></p><pre class="brush:as3;toolbar:false">&lt;?php namespace&nbsp;MyVendor\MyModule\Model; use&nbsp;Magento\Framework\ObjectManagerInterface; class&nbsp;MyClass { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$objectManager; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct(ObjectManagerInterface&nbsp;$objectManager) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;objectManager&nbsp;=&nbsp;$objectManager; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;createObject() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$object&nbsp;=&nbsp;$this-&gt;objectManager-&gt;create(MyObjectFactory::class)-&gt;create(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$object; &nbsp;&nbsp;&nbsp;&nbsp;} } class&nbsp;MyObjectFactory { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$objectManager; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct(ObjectManagerInterface&nbsp;$objectManager) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;objectManager&nbsp;=&nbsp;$objectManager; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;create() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$object&nbsp;=&nbsp;$this-&gt;objectManager-&gt;create(MyObject::class); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$object; &nbsp;&nbsp;&nbsp;&nbsp;} } class&nbsp;MyObject { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;doSomething() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Your&nbsp;custom&nbsp;code&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>在上面的示例中,MyClass 类使用工厂类来创建 MyObject 类的实例。MyClass 类中的 createObject() 方法使用 MyObjectFactory 类来创建 MyObject 类的实例。</p><p><br/></p><p>MyObjectFactory 类中的 create() 方法实际上创建 MyObject 类的实例,并返回该实例。 这种方式可以保持类之间的松散耦合。</p><p><br/></p><p>请注意,在使用工厂类时,最好将其注入到类的构造函数中,并且不要直接使用 new 操作符创建对象。</p><p><br/></p>

magento2中的事件和观察者以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中,事件和观察者是常见的设计模式之一,用于在Magento的核心代码中插入自定义代码。它们使得在不修改核心代码的情况下,您可以在Magento的不同点添加自定义代码。</p><p><br/></p><p>以下是使用事件和观察者的简单示例:</p><p><br/></p><p>定义事件</p><pre class="brush:as3;toolbar:false">use&nbsp;Magento\Framework\Event; class&nbsp;MyCustomEvent&nbsp;extends&nbsp;Event { &nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;EVENT_NAME&nbsp;=&nbsp;&#39;my_custom_event&#39;; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::__construct(self::EVENT_NAME,&nbsp;$data); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>触发事件</p><pre class="brush:as3;toolbar:false">use&nbsp;Magento\Framework\Event\ManagerInterface; class&nbsp;MyCustomClass { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$eventManager; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ManagerInterface&nbsp;$eventManager &nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;eventManager&nbsp;=&nbsp;$eventManager; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;doSomething() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eventName&nbsp;=&nbsp;MyCustomEvent::EVENT_NAME; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eventData&nbsp;=&nbsp;[&#39;data&#39;&nbsp;=&gt;&nbsp;&#39;my&nbsp;custom&nbsp;data&#39;]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;eventManager-&gt;dispatch($eventName,&nbsp;$eventData); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>定义观察者</p><pre class="brush:as3;toolbar:false">use&nbsp;Magento\Framework\Event\ObserverInterface; class&nbsp;MyCustomObserver&nbsp;implements&nbsp;ObserverInterface { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;execute(\Magento\Framework\Event\Observer&nbsp;$observer) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Your&nbsp;custom&nbsp;code&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>注册观察者</p><pre class="brush:as3;toolbar:false">&lt;config&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:Event/etc/events.xsd&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;event&nbsp;name=&quot;my_custom_event&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;observer&nbsp;name=&quot;my_custom_observer&quot;&nbsp;instance=&quot;MyVendor\MyModule\Observer\MyCustomObserver&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/event&gt; &lt;/config&gt;</pre><p>在上面的示例中,我们定义了一个名为 MyCustomEvent 的事件,并在 MyCustomClass 类中触发该事件。</p><p><br/></p><p>我们还定义了一个名为 MyCustomObserver 的观察者,并在 events.xml 文件中注册它,以便在事件 my_custom_event 触发时执行它。</p><p><br/></p><p>请注意,在观察者中实现的 execute() 方法将包含您想要在事件触发时执行的所有自定义代码。</p><p><br/></p>

magento2中的对象管理器助手以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中,对象管理器(Object Manager)是一个用于创建和管理类实例的核心组件。您可以通过依赖注入或使用对象管理器助手(Object Manager Helper)获取对象管理器的实例。</p><p><br/></p><p>以下是一个简单的示例:</p><p><br/></p><pre class="brush:as3;toolbar:false">use&nbsp;Magento\Framework\App\ObjectManager; use&nbsp;Magento\Catalog\Api\ProductRepositoryInterface; class&nbsp;MyCustomClass { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$productRepository; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$objectManager&nbsp;=&nbsp;ObjectManager::getInstance(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;productRepository&nbsp;=&nbsp;$objectManager-&gt;get(ProductRepositoryInterface::class); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getProductById($id) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&gt;productRepository-&gt;getById($id); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>在上面的示例中,我们创建了一个名为 MyCustomClass 的类,并使用对象管理器助手获取了 ProductRepositoryInterface 的实例。我们还在构造函数中初始化了 $productRepository 属性,并使用该属性来获取指定 $id 的产品。</p><p><br/></p><p>请注意,使用对象管理器助手在Magento 2中是不推荐的。它应该只用于临时解决方案和快速的原型开发。推荐使用依赖注入来获取所需的类实例。</p><p><br/></p>

magento2中的对象管理器以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中,对象管理器(Object Manager)是一个用于创建和管理类实例的核心组件。您可以使用对象管理器来获取Magento 2中的各种类的实例。以下是一个简单的示例:</p><p><br/></p><pre class="brush:as3;toolbar:false">use&nbsp;Magento\Framework\App\ObjectManager; use&nbsp;Magento\Catalog\Api\ProductRepositoryInterface; class&nbsp;MyCustomClass { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$productRepository; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$objectManager&nbsp;=&nbsp;ObjectManager::getInstance(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;productRepository&nbsp;=&nbsp;$objectManager-&gt;get(ProductRepositoryInterface::class); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getProductById($id) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&gt;productRepository-&gt;getById($id); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>在上面的示例中,我们创建了一个名为 MyCustomClass 的类,并使用对象管理器获取了 ProductRepositoryInterface 的实例。我们还在构造函数中初始化了 $productRepository 属性,并使用该属性来获取指定 $id 的产品。</p><p><br/></p><p>虽然使用对象管理器可以方便地获取Magento 2中的类的实例,但它不是最佳实践。因为使用对象管理器会导致代码耦合,使代码难以测试和维护。Magento 2推荐使用依赖注入来获取所需的类实例,而不是使用对象管理器。</p><p><br/></p>

magento2中的依赖注入以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中,依赖注入是一种设计模式,用于从对象本身的构造函数中获取所需的依赖项,而不是通过对象管理器或其他方式进行实例化。</p><p><br/></p><p>以下是一个简单的示例:</p><pre class="brush:as3;toolbar:false"> use&nbsp;Magento\Catalog\Api\ProductRepositoryInterface; class&nbsp;MyCustomClass { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$productRepository; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct(ProductRepositoryInterface&nbsp;$productRepository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;productRepository&nbsp;=&nbsp;$productRepository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getProductById($id) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&gt;productRepository-&gt;getById($id); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>在上面的示例中,我们创建了一个名为 MyCustomClass 的类,并在构造函数中声明了 ProductRepositoryInterface 依赖项。通过这种方式,我们可以使用依赖注入来传递 ProductRepositoryInterface 实例,并将其存储在 $productRepository 属性中。</p><p><br/></p><p>使用依赖注入可以帮助我们减少代码耦合,使代码更容易测试和维护。同时,在Magento 2中,依赖注入还可以使用XML配置文件和自动生成的代码来自动注入所需的依赖项。</p><p><br/></p>

magento2中的公共接口和 API以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中,公共接口和API是用于与Magento 2的其他系统或第三方应用程序进行交互的关键组件。它们允许您从外部应用程序中访问Magento 2的核心功能和数据。</p><p><br/></p><p>以下是一个简单的示例:</p><p><br/></p><pre class="brush:as3;toolbar:false">use&nbsp;Magento\Framework\Api\SearchCriteriaBuilder; use&nbsp;Magento\Customer\Api\CustomerRepositoryInterface; class&nbsp;MyCustomClass { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$customerRepository; &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$searchCriteriaBuilder; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct(CustomerRepositoryInterface&nbsp;$customerRepository,&nbsp;SearchCriteriaBuilder&nbsp;$searchCriteriaBuilder) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;customerRepository&nbsp;=&nbsp;$customerRepository; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;searchCriteriaBuilder&nbsp;=&nbsp;$searchCriteriaBuilder; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getCustomerByEmail($email) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$searchCriteria&nbsp;=&nbsp;$this-&gt;searchCriteriaBuilder-&gt;addFilter(&#39;email&#39;,&nbsp;$email)-&gt;create(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$customerList&nbsp;=&nbsp;$this-&gt;customerRepository-&gt;getList($searchCriteria)-&gt;getItems(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(count($customerList)&nbsp;&gt;&nbsp;0)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$customerList[0]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>在上面的示例中,我们创建了一个名为 MyCustomClass 的类,并在构造函数中注入了 CustomerRepositoryInterface 和 SearchCriteriaBuilder。通过这些接口,我们可以获取指定电子邮件的客户。</p><p><br/></p><p>值得注意的是,API和公共接口在Magento 2中使用相同的接口类型。它们只是根据使用方式和访问权限进行分类。例如,CustomerRepositoryInterface 是公共接口,因此只能用于在Magento 2中运行的代码中,而不是在外部应用程序中使用。</p><p><br/></p><p>如果您想要使用Magento 2的API来与外部应用程序进行交互,则可以使用Web API。Web API 允许您使用HTTP请求(例如GET、POST、PUT和DELETE)来调用Magento 2的功能和服务。要使用Web API,您需要首先在Magento 2后端配置Web API访问和授权,并为每个要使用的API端点生成令牌。然后,您可以使用curl或其他HTTP客户端来调用API端点。以下是一个简单的示例:</p><p><br/></p><p><br/></p><p>curl -X GET <span style="color: #ce9178;">&quot;http://magento2-base-url/rest/V1/products?searchCriteria[pageSize]=10&quot;</span> -H <span style="color: #ce9178;">&quot;Authorization: Bearer &lt;token&gt;&quot;</span></p><p>在上面的示例中,我们使用curl来调用Magento 2的Web API,以获取前10个产品。我们在请求标头中包含了一个有效的令牌,以便进行身份验证和授权。</p><p><br/></p>

magento2中的服务契约设计模式以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>服务契约设计模式是一种基于接口的设计模式,用于定义服务的行为和数据结构。在Magento 2中,服务契约用于定义模块之间的接口,以便实现松散耦合和更好的可扩展性。</p><p><br/></p><p>Magento 2中的服务契约通常使用Interface后缀命名。例如,以下是一个定义了一个计算器服务的接口:</p><p><br/></p><pre class="brush:as3;toolbar:false">namespace&nbsp;MyVendor\MyModule\Api; interface&nbsp;CalculatorInterface { &nbsp;&nbsp;&nbsp;&nbsp;/** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Adds&nbsp;two&nbsp;numbers&nbsp;together&nbsp;and&nbsp;returns&nbsp;the&nbsp;result. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;float&nbsp;$a&nbsp;The&nbsp;first&nbsp;number&nbsp;to&nbsp;add. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;float&nbsp;$b&nbsp;The&nbsp;second&nbsp;number&nbsp;to&nbsp;add. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;float&nbsp;The&nbsp;sum&nbsp;of&nbsp;the&nbsp;two&nbsp;numbers. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;add($a,&nbsp;$b); }</pre><p>在上面的代码中,我们定义了一个名为 CalculatorInterface 的接口,并定义了一个名为 add() 的方法,该方法接受两个参数并返回它们的和。请注意,我们在方法注释中提供了有关方法行为的详细说明。这些注释非常重要,因为它们可以让其他开发人员更容易地了解如何使用您的服务。</p><p><br/></p><p>接口定义后,我们可以通过实现该接口的类来提供服务。以下是一个实现 CalculatorInterface 接口的示例:</p><p><br/></p><pre class="brush:as3;toolbar:false">namespace&nbsp;MyVendor\MyModule\Model; use&nbsp;MyVendor\MyModule\Api\CalculatorInterface; class&nbsp;Calculator&nbsp;implements&nbsp;CalculatorInterface { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;add($a,&nbsp;$b) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$a&nbsp;+&nbsp;$b; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>在上面的代码中,我们创建了一个名为 Calculator 的类,并实现了 CalculatorInterface 接口中定义的 add() 方法。这个方法执行加法操作并返回结果。</p><p><br/></p><p>现在,我们可以使用Magento 2的对象管理器来访问我们的服务。以下是一个使用 Calculator 服务的示例:</p><p><br/></p><pre class="brush:as3;toolbar:false">use&nbsp;MyVendor\MyModule\Api\CalculatorInterface; use&nbsp;Magento\Framework\App\Action\Action; class&nbsp;MyCustomAction&nbsp;extends&nbsp;Action { &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$calculator; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct(CalculatorInterface&nbsp;$calculator) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;calculator&nbsp;=&nbsp;$calculator; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;execute() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result&nbsp;=&nbsp;$this-&gt;calculator-&gt;add(2,&nbsp;3); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;The&nbsp;result&nbsp;is:&nbsp;&quot;&nbsp;.&nbsp;$result; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>在上面的代码中,我们创建了一个名为 MyCustomAction 的类,并注入了 CalculatorInterface 接口。我们在 execute() 方法中调用 Calculator 服务的 add() 方法,并将结果打印到屏幕上。</p><p><br/></p><p>使用服务契约设计模式可以使代码更易于理解、测试和维护。它使您的代码更加灵活和可扩展,因为它们能够轻松地与其他模块和第三方应用程序进行交互。</p><p><br/></p>