我在控制器中有一个这样的方法.事实上,我需要声明一个函数checkLogin,以便我可以在几个Controller方法中使用,如下所示:
class DefaultController extends Controller
{
/**
* @Route("/test")
* @Template()
*/
public function testAction()
{
if (checkLogin()){}
else {}
exit;
}
public static function checkLogin()
{
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,当我这样做时,我收到以下错误:
致命错误:在第142行的/home/noor/noupei/website/WebSiteBundle/Controller/DefaultController.php中调用未定义的函数NouPei\WebSiteBundle\Controller\checkLogin()
小智 5
对于Symfony 2中的帐户管理,您应该使用symfony 2的安全管理(此处).您将能够像这样检查用户登录:
public function indexAction()
{
// show different content to admin users
if ($this->get('security.context')->isGranted('ADMIN')) {
// Load admin content here
}
// load other regular content here
}
Run Code Online (Sandbox Code Playgroud)
如果您不想使用Symfony 2安全管理,则应使用服务为每个控制器提供方法.
| 归档时间: |
|
| 查看次数: |
23534 次 |
| 最近记录: |