4

I am new into Phalcon framework. I just got the basic idea about it. Every controller has methods with multiple specific actions. I wrote a huge indexAction method but now I want to break it down with multiple private method so that I can reuse those functionality. But when I try to create any method without action suffix, it returns error(Page Not Found).
How can I break it down into multiple methods?

sz ashik
  • 881
  • 7
  • 20

3 Answers3

2
<?php

use Phalcon\Mvc\Controller;

class PostsController extends Controller
{
    public function indexAction()
    {
        $this->someMethod();
    }

    public function someMethod()
    {
        //do your things
    }
}
Saleh Ahmad Oyon
  • 672
  • 1
  • 6
  • 20
0

Controllers must have the suffix “Controller” while actions the suffix “Action”. A sample of a controller is as follows:

<?php

use Phalcon\Mvc\Controller;

class PostsController extends Controller
{
    public function indexAction()
    {

    }

    public function showAction($year, $postTitle)
    {

    }
}

For calling another method, you would use it straight forward

<?php

use Phalcon\Mvc\Controller;

class PostsController extends Controller
{
    public function indexAction()
    {
        echo $this->showAction();
    }

    private function showAction()
    {
        return "show";
    }
}    

Docs.

Tpojka
  • 6,996
  • 2
  • 29
  • 39
  • I understand that i have to create actions with Action suffix. But I want to break down my indexAction into multiple methods so that I can reduce the size of indexAction method and reuse those methods for next time – sz ashik Apr 23 '16 at 12:51
0

What exactly do you want? The answer seems trivial to me.

class YourController extends Phalcon\Mvc\Controller
{
    // this method can be called externally because it has the "Action" suffix
    public function indexAction()
    {
       $this->customStuff('value');
       $this->more();
    }

    // this method is only used inside this controller
    private function customStuff($parameter)
    {

    }

    private function more()
    {

    }
}
Timothy
  • 2,004
  • 3
  • 23
  • 29