0

I need to call a controller function in my view and passing parameter. I tried to following this How to call controller function in view in Zend Framework? but it's still not working.

I have record from my database like this :

---------------
| name  | age |
---------------
| Josh  | 22  |
| Bush  | 43  |
| Rush  | 23  |
---------------

here's my index.phtml

foreach ($result as $rstd){
    echo "<td>".$this->escapeHtml($rstd['name'])."</td>";
    echo "<td>".$this->escapeHtml($rstd['age'])."</td>";

    //here i want to access my controller function with sending parameter by name and also display something which has i set in that function.
    echo "<td>** result from that function **</td>";
}

here's my controller :

public function indexAction(){
    $result = $sd->getAllRecord($this->getMysqlAdapter());
    return new ViewModel(array('result'=>$result));
}

public function getRecordByName($name){
    if($name=='Bush'){
        $result = "You'r Old";  
    }else{
        $result = "You'r Young";    
    }

    return $result;
}

And i want display it like this :

-----------------------------
| name  | age | status      |
-----------------------------
| Josh  | 22  | You'r Young |
| Bush  | 43  | You'r Old   |
| Rush  | 32  | You'r Young |
-----------------------------

Can you help me ?

4
  • whatever you have done in getRecordByName if you want to do the same you can do it in views directly. but it seems you are looking to write some common functions that you could use in views ? Commented Apr 2, 2015 at 9:27
  • and why you believe that Zend 1 solution will work for Zend Framework 2 ? Also that wash half baked solution as its recommended to make view helper then pass whole controller object) Commented Apr 2, 2015 at 9:28
  • @Richie yeah, getRecordByName just a simple example . Actually I would make something complex in that function. And i can't do the same in view coz it's so difficult to implements, and it's can be work only in controller not in view. So do you have a solution for this ..? Commented Apr 2, 2015 at 9:54
  • zend 3 question: How can we fetch cms pages title and their links to display them in header / footer sections of entire website. Pages title and links are stored in database. Please share any suggestion. Thanks. Commented Feb 25, 2020 at 14:02

2 Answers 2

2

Call a controller action in view in considered bad practice. But you could achieve that by using view helpers. So what you need is:

  • Create your custom View Helper,
  • Register the view helper in invokables in your module.config.php,
  • Then you could call any controller action in your views

Here is a helper that you could use :

class Action extends \Zend\View\Helper\AbstractHelper implements   ServiceLocatorAwareInterface
{

    protected $serviceLocator;
    
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
        return $this;
    }
    
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
    public function __invoke($controllerName, $actionName, $params = array())
    {
        $controllerLoader = $this->serviceLocator->getServiceLocator()->get('ControllerLoader');
        $controllerLoader->setInvokableClass($controllerName, $controllerName);
        $controller = $controllerLoader->get($controllerName);
        return $controller->$actionName($params);
    }
}

module.config.php :

'view_helpers' => array(
'invokables' => array(
    'action' => 'module_name\View\Helper\Action',
),  
),

In your view file :

$this->action('Your\Controller', 'getRecordByNameAction');
Sign up to request clarification or add additional context in comments.

1 Comment

zend 3 question: How can we fetch cms pages title and their links to display them in header / footer sections of entire website. Pages title and links are stored in database. Please share any suggestion. Thanks.
0

As per the comments you need to implement viewhelpers I have found a very easy solution here. This might be useful for you too.

ZF2 - How can i call a function of a custom class php from a view?

and here

https://samsonasik.wordpress.com/2012/07/20/zend-framework-2-create-your-custom-view-helper/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.