We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

access controller in view

this is my controller

<?php
namespace App\Controllers\Admin;

class SettingAccessController extends ControllerBase
{
  public function indexAction() 
  {
        //something
  }

  pubic function check($param1, $param2)
  {
          if (something) {
              return true;
          }
          return false
  }
}

and I want use "check" method in my view ? I have tried this way. but I get error

Macro 'check' does not exist

    {% if check(param1, param2) %}
        //do something
    {% endif %}

how to make method can access in view ?

edited Apr '16

You have two options:

1) in your current controller method you create a variable and call the check method:

  public function indexAction() 
  {
        $this->view->variable = $this->check();
  }

And in volt:

{% if variable %}

{% endif %}

2) The better solution is to create custom volt function. More info here: https://docs.phalcon.io/en/latest/reference/volt.html#extending-volt

edited Nov '17

**>You have two options:

1) in your current controller method you create a variable and call the check method:

 public function indexAction() 
 {
       $this->view->variable = $this->check();
 }

And in volt:

{% if variable %}

{% endif %}

2) The better solution is to create custom volt function. More info here: https://docs.phalcon.io/en/latest/reference/volt.html#extending-volt**

Hey, I am not using volt, I am using phtml for views. How can I access variable from controller using method 1?

@Maulik,

<?php if ($variable): ?>

<?php endif; ?>

More info here: https://olddocs.phalcon.io/en/3.0.3/reference/views.html