【发布时间】:2016-04-23 12:17:38
【问题描述】:
我是 Phalcon 框架的新手。我只是对它有了基本的了解。每个控制器都有具有多个特定操作的方法。我写了一个巨大的 indexAction 方法,但现在我想用多个私有方法分解它,以便我可以重用这些功能。但是,当我尝试创建任何没有操作后缀的方法时,它会返回错误(找不到页面)。
如何将其分解为多个方法?
【问题讨论】:
我是 Phalcon 框架的新手。我只是对它有了基本的了解。每个控制器都有具有多个特定操作的方法。我写了一个巨大的 indexAction 方法,但现在我想用多个私有方法分解它,以便我可以重用这些功能。但是,当我尝试创建任何没有操作后缀的方法时,它会返回错误(找不到页面)。
如何将其分解为多个方法?
【问题讨论】:
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
$this->someMethod();
}
public function someMethod()
{
//do your things
}
}
【讨论】:
控制器必须具有后缀“Controller”而动作必须具有后缀“Action”。控制器示例如下:
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
}
public function showAction($year, $postTitle)
{
}
}
要调用另一个方法,你可以直接使用它
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
echo $this->showAction();
}
private function showAction()
{
return "show";
}
}
Docs.
【讨论】:
你到底想要什么?答案对我来说似乎微不足道。
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()
{
}
}
【讨论】: