Skip to content

Commit

Permalink
Inyección de dependencia
Browse files Browse the repository at this point in the history
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
PerezContrerasLuis committed Apr 19, 2022
1 parent bd7582f commit c0e784e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
10 changes: 5 additions & 5 deletions code/Luisdev/Inyeccion/Controller/Index/Index.php
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();
}
}
8 changes: 8 additions & 0 deletions code/Luisdev/Inyeccion/NotMagento/BigPencil.php
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";
}
}
6 changes: 6 additions & 0 deletions code/Luisdev/Inyeccion/NotMagento/PencilInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Luisdev\Inyeccion\NotMagento;

interface PencilInterface {
public function getTypePencil();
}
8 changes: 8 additions & 0 deletions code/Luisdev/Inyeccion/NotMagento/RedPencil.php
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";
}
}
8 changes: 8 additions & 0 deletions code/Luisdev/Inyeccion/NotMagento/YellowPencil.php
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";
}
}
11 changes: 11 additions & 0 deletions code/Luisdev/Inyeccion/etc/frontend/di.xml
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>

0 comments on commit c0e784e

Please sign in to comment.