Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend框架如何做到这一点,以免自己重复_Php_Oop_Zend Framework - Fatal编程技术网

Php Zend框架如何做到这一点,以免自己重复

Php Zend框架如何做到这一点,以免自己重复,php,oop,zend-framework,Php,Oop,Zend Framework,我在多个地方都有我需要的东西: public function init() { $fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session if(!$fbLogin->user) $this->_redirect('/'); #Logout the user } 这两条线: $fbLogin = new Zend_Session_Namespace('fbLogin');

我在多个地方都有我需要的东西:

public function init()
{
    $fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session
    if(!$fbLogin->user) $this->_redirect('/'); #Logout the user
}
这两条线:

    $fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session
    if(!$fbLogin->user) $this->_redirect('/'); #Logout the user
在ZendFramework中最好的方法是什么?创建一个插件还是?我的意思是我想在多个地方执行它,但如果我需要编辑它,我想在一个地方编辑它。

这里是一个可以从控制器轻松调用的示例

<?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;
    }
}
然后在控制器中使用它:

public function preDispatch()
{
    $this->_helper->CheckFbLogin(); // redirects if not logged in
}
它不太详细,但也很有帮助。

这里是一个可以从控制器轻松调用的示例

<?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;
    }
}
然后在控制器中使用它:

public function preDispatch()
{
    $this->_helper->CheckFbLogin(); // redirects if not logged in
}

它不太详细,但也很有用。

如果您需要在每个控制器中进行此检查,您甚至可以设置一个baseController,从中扩展而不是默认控制器:

class My_Base_Controller extends Zend_Controller_Action
{ 
    public function init()
    { ...

init()
移动到基本控制器中,您不需要在每个特定控制器中重复自己的操作

需要在特定控制器中使用不同的
init()

class FooController extends My_Base_Controller
{
    public function init()
    {
        parent::init();
        ...

如果您需要在每个控制器中进行此检查,您甚至可以设置一个baseController,从中扩展,而不是默认的baseController:

class My_Base_Controller extends Zend_Controller_Action
{ 
    public function init()
    { ...

init()
移动到基本控制器中,您不需要在每个特定控制器中重复自己的操作

需要在特定控制器中使用不同的
init()

class FooController extends My_Base_Controller
{
    public function init()
    {
        parent::init();
        ...

文件夹结构是什么?Controller/Action/Helper/My_Helper.php?我从中获取该示例的项目具有结构,
library/My/Helper/CheckFbLogin.php
Ok,谢谢我会尝试一下,我从来都不理解ZF文件夹结构:))现在我遇到了这个错误:
致命错误:未捕获异常“Zend_Controller_Action_exception”和消息“Method”\u redirect”在
中不存在并且没有被困在uu call()中,我使用
$this->getActionController()->getHelper('redirector')->GoTour('/')解决了这个问题文件夹结构是什么?Controller/Action/Helper/My_Helper.php?我从中获取该示例的项目具有结构,
library/My/Helper/CheckFbLogin.php
Ok,谢谢我会尝试一下,我从来都不理解ZF文件夹结构:))现在我遇到了这个错误:
致命错误:未捕获异常“Zend_Controller_Action_exception”和消息“Method”\u redirect”在
中不存在并且没有被困在uu call()中,我使用
$this->getActionController()->getHelper('redirector')->GoTour('/')解决了这个问题