-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ojo la estructura creada en este modulo es únicamente para fines ilustrativos ya que no corresponden con la estructura recomendada por magento. Objetivo : entender que es la inyección de dependencia y cómo se emplea en magento2. 1.- Creamos interfaz para pencil : app/code/Luisdev/Inyeccion/NotMagento/PencilInterface.php 2.- Creamos nuestras clases concretas que implementan la interfaz ainterior : app/code/Luisdev/Inyeccion/NotMagento/YellowPencil.php app/code/Luisdev/Inyeccion/NotMagento/RedPencil.php 3.- Creamos nuestro archivo de dependencia de inyección (acuerdo) en donde definimos la interfaz y la clase especifica quesera inyectada al llamar a la interfaz. app/code/Luisdev/Inyeccion/etc/frontend/di.xml 4.- Ahora INYECTAMOS nuestra interfaz en nuestro controlador Index: app/code/Luisdev/Inyeccion/Controller/Index/Index.php 5.- corremos comandos y consultamos url : http://magento240.local/inyeccion/index OJO en el caso de querer usar la clase red o la clase Big tendríamos que modificar nuestro archivo di.xml”
- Loading branch information
1 parent
bd7582f
commit c0e784e
Showing
6 changed files
with
46 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
<?php | ||
namespace Luisdev\Inyeccion\Controller\Index; | ||
use Luisdev\Inyeccion\NotMagento\PencilInterface; | ||
class Index extends \Magento\Framework\App\Action\Action | ||
{ | ||
protected $_pageFactory; | ||
protected $pencilInterface; | ||
public function __construct( | ||
\Magento\Framework\App\Action\Context $context, | ||
\Magento\Framework\View\Result\PageFactory $pageFactory) | ||
\Magento\Framework\App\Action\Context $context,PencilInterface $pencilInterface) | ||
{ | ||
$this->_pageFactory = $pageFactory; | ||
$this->pencilInterface = $pencilInterface; | ||
return parent::__construct($context); | ||
} | ||
public function execute() | ||
{ | ||
//return $this->_pageFactory->create(); | ||
echo "controlador Inyection"; | ||
echo $this->pencilInterface->getTypePencil(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
namespace Luisdev\Inyeccion\NotMagento; | ||
|
||
class BigPencil implements PencilInterface{ | ||
public function getTypePencil(){ | ||
return "this is a big pencil"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
namespace Luisdev\Inyeccion\NotMagento; | ||
|
||
interface PencilInterface { | ||
public function getTypePencil(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
namespace Luisdev\Inyeccion\NotMagento; | ||
|
||
class RedPencil implements PencilInterface { | ||
public function getTypePencil() { | ||
return "RedPencil"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
namespace Luisdev\Inyeccion\NotMagento; | ||
|
||
class YellowPencil implements PencilInterface{ | ||
public function getTypePencil(){ | ||
return "YellowPencil"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="Luisdev\Inyeccion\NotMagento\PencilInterface" | ||
type="Luisdev\Inyeccion\NotMagento\YellowPencil" /> | ||
</config> |