Uff*_*ffo 0 php oop zend-framework
我在多个地方都需要这个东西:
public function init()
{
$fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session
if(!$fbLogin->user) $this->_redirect('/'); #Logout the user
}
Run Code Online (Sandbox Code Playgroud)
这两行:
$fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session
if(!$fbLogin->user) $this->_redirect('/'); #Logout the user
Run Code Online (Sandbox Code Playgroud)
什么是ZendFramework中最好的方法?创建插件或?我的意思是我想在多个地方执行它但是如果我需要编辑它我想在一个地方编辑它.
以下是可以轻松从控制器调用的Action Helper示例.
<?php
class My_Helper_CheckFbLogin extends Zend_Controller_Action_Helper_Abstract
{
public function direct(array $params = array())
{
// you could pass in $params as an array and use any of its values if needed
$request = $this->getRequest();
$view = $this->getActionController()->view;
$fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session
if(!$fbLogin->user) {
$this->getActionController()
->getHelper('redirector')
->gotoUrl('/'); #Logout the user
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
为了使用它,您必须告诉助手经纪人它将居住在哪里.下面是一个示例代码,您可以在引导程序中执行此操作:
// Make sure the path to My_ is in your path, i.e. in the library folder
Zend_Loader_Autoloader::getInstance()->registerNamespace('My_');
Zend_Controller_Action_HelperBroker::addPrefix('My_Helper');
Run Code Online (Sandbox Code Playgroud)
然后在您的控制器中使用它:
public function preDispatch()
{
$this->_helper->CheckFbLogin(); // redirects if not logged in
}
Run Code Online (Sandbox Code Playgroud)
它没有详细介绍,但编写自己的助手也很有帮助.