【发布时间】:2014-04-05 14:38:52
【问题描述】:
我正在构建几页应用程序。想问一下控制器的正确使用方法是什么?
每个页面都有一个控制器?或者将页面作为方法放在同一个控制器中?就我而言,我正在使用数据库,我真的无法将所有内容都保存在一种方法中。在这种情况下,我创建了 Helper 类,帮助我在那里保存和生成一些代码。
控制器:
class DefaultController extends Controller
{
//main method that performs as page
public function indexAction()
{
$helper = new IgnasHelper();
$profile = $this->profileQuery($helper);
return $this->render(
'IgnasIgnasBundle:Default:index.html.twig',
array('profile' => $profile)
);
}
//method that returns database's data back to main index method
public function profileQuery(IgnasHelper $helper)
{
$em = $this->getDoctrine()->getManager();
$selectAll = array('p.id', 'p.first', 'p.last', 'p.birth', 'p.country', 'p.city', 'p.email');
$profile = $em->createQueryBuilder()
->select($selectAll)
->from('IgnasIgnasBundle:Profilis', 'p')
->getQuery()
->getResult();
return $helper->profileArray($profile);
}
}
现在是 Helper 类:
public function profileArray(array $profile)
{
$id = $profile[0]['id'];
$first = $profile[0]['first'];
$last = $profile[0]['last'];
$birth = $profile[0]['birth'];
$country = $profile[0]['country'];
$city = $profile[0]['city'];
$email = $profile[0]['email'];
return array(
'id' => $id,
'first' => $first,
'last' => $last,
'birth' => $birth,
'country' => $country,
'city' => $city,
'email' => $email,
);
}
所以对于其他页面,我正在考虑制作另一个控制器来执行它。我是否正确使用了控制器?
【问题讨论】:
-
对于你的 profileArray 函数,你只能做 return $profile[0];没有别的
-
是的,你是对的,我会这样做的 :)
标签: php symfony controller