Magento 2 - Calling functions from a block from controller

Magento 2 - Calling functions from a block from controller



Say I want to call a function from a Block while inside my Controller class which outputs on screen, how would i go about this?



Code for New/Module/Controller/Hello/World.php :


<?php
namespace New/Module/Controller/Hello/;
class World extends MagentoFrameworkAppActionAction


public function execute()

echo '<p>"Hello World"</p>';
var_dump(__METHOD__);

divide(10,1); <-- trying to do this




Code for New/Module/Block/Calculator.php


<?php
namespace NewModuleBlock;
class Calculator
public function divide($number1, $number2)

return $number1/$number2;

public function multiply($number1,$number2)
return $number1*$number2;

public function add($number1,$number2)
return $number1+$number2;

public function sub($number1,$number2)
return $number1-$number2;




I'm attempting to create a calculator and I'm not even sure this is the best way to go about calling functions to output on screen.



Any help would be much appreciated :)




2 Answers
2



You can call block methods by creating instance of that class.


<?php
namespace New/Module/Controller/Hello/;
class World extends MagentoFrameworkAppActionAction



/**
* Calculator
*
* @var NewModuleBlockCalculator
*/
public $calculator;

public function __construct(
MagentoFrameworkAppActionContext $context,
NewModuleBlockCalculator $calculator
)
$this->calculator = $calculator;
return parent::__construct($context);


public function execute()

echo '<p>"Hello World"</p>';
var_dump(__METHOD__);

//divides(10,1); <-- trying to do this

$this->calculator->divide(10,1);







I answered you quickly why you didn't accept.

– mahendra j
Sep 7 '18 at 12:23



Use Below code for it


<?php
namespace New/Module/Controller/Hello/;
class World extends MagentoFrameworkAppActionAction

protected $_calculator;

public function __construct(
MagentoFrameworkAppActionContext $context,
NewModuleBlockCalculator $calculator
)
$this->_calculator=$calculator;
parent::__construct($context);

public function execute()

echo '<p>"Hello World"</p>';
var_dump(__METHOD__);

$this->_calculator->divide(10,1); <-- trying to do this







Say i wanted to add a multiply method in the Calculator class, how would i then call both ?

– Greg
Sep 7 '18 at 11:31






<?php namespace NewModuleBlock; class Calculator public function divide($number1, $number2) return $number1/$number2; public function multiply($number1, $number2) return $number1/$number2; $this->_calculator->multiply(10,1)

– Ansar Husain
Sep 7 '18 at 11:34






When i print, echo $this->calculator->divide(10,5); echo $this->calculator->multiply(10,5); i only get one number. Why?

– Greg
Sep 7 '18 at 11:38






sorry use public function multiply($number1, $number2) return $number1*$number2;

– Ansar Husain
Sep 7 '18 at 11:42






You need to add echo "<br/>"; after each function in controller

– Ansar Husain
Sep 7 '18 at 11:59



Thanks for contributing an answer to Magento Stack Exchange!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)