Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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/6/codeigniter/3.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 如何在通用任务的CodeIgniter中实现观察者模式_Php_Codeigniter_Observer Pattern - Fatal编程技术网

Php 如何在通用任务的CodeIgniter中实现观察者模式

Php 如何在通用任务的CodeIgniter中实现观察者模式,php,codeigniter,observer-pattern,Php,Codeigniter,Observer Pattern,我正在经典的CI mvc设置中构建一个应用程序,其中用户有一个常规/通用任务列表。 任务的主要目的是指示用户必须完成特定操作,并将其重定向到需要完成此操作的页面 以一种非常简单的方式,任务的db方案如下所示: 任务列表本身就是一个重定向用户的列表: 我的问题是,当用户被重定向到需要执行操作的特定页面时,我们会丢失特定任务的上下文。因此,即使任务已完成(在本例中,文档已上载),任务本身也不知道这一点,我们也没有更新任务的连接 经过一些研究,观察器设计模式看起来就是能够满足这种需求的模式。但是,

我正在经典的CI mvc设置中构建一个应用程序,其中用户有一个常规/通用任务列表。 任务的主要目的是指示用户必须完成特定操作,并将其重定向到需要完成此操作的页面

以一种非常简单的方式,任务的db方案如下所示:

任务列表本身就是一个重定向用户的列表:

我的问题是,当用户被重定向到需要执行操作的特定页面时,我们会丢失特定任务的上下文。因此,即使任务已完成(在本例中,文档已上载),任务本身也不知道这一点,我们也没有更新任务的连接

经过一些研究,观察器设计模式看起来就是能够满足这种需求的模式。但是,通过所有的例子,我都没能对如何在我们当前的系统中真正实现这一点进行讨论

在处理文档上传的控制器中,有一个函数upload_doc(){},该函数成功执行后,还应更新连接或订阅此文档上传的任务

class Dashboard extends MY_Controller{

public function __construct()
{
    parent::__construct();

    // Force SSL
    $this->force_ssl();
}

public function upload_doc(){
   //Handle doc upload and update task
}
}
谁能以一种不友好的方式帮助我如何在CI框架内实现这种设置


提前谢谢

如果涉及到设计模式,我总是试图找到一份参考文档/github repo,其中包含所需语言的设计模式示例。对于PHP,我可以在此热烈推荐:

示例实现可能如下所示。注意:我没有使用CodeIgniter的经验。这只是说明如何使用给定的代码示例实现此功能的一种方法

class Dashboard extends MY_Controller 
{
    private function makeFile()
    {
        // I would put this method into a file factory within your container.
        // This allows you to extend on a per module-basis.

        $file = new File();        
        $file->attach(new UpdateTask);
        $file->attach(new DeleteFileFromTempStorage);
        $file->attach(new IncrementStorageSize);
        $file->attach(new AddCustomerNotification);
        return $file;
    }

    public function upload_doc() 
    {
        // My expectation is that you have some task-id reference 
        // when uploading a file. This would allow all observers 
        // to "find" the right task to update.
        $file = $this->newFile();

        // enhance your file model with your request parameters
        $file->fill($params);

        // save your file. Either your model has this functionality already
        // or you have a separated repository which handles this for you.
        $fileRepo->persist($file);

        // Finally notify your observers
        $file->notify();  
    }
}

希望这能有所帮助。

如果我正确理解您的问题和意图(在一个地方创建的记录与其他地方发生的某些操作之间建立关系),那么Observer(如果我们引用同一本书中的相同想法)将无法解决它,它甚至不适用于这里,因为PHP和web的一般无状态性质,即跨多个程序调用执行。GoF书中的经典模式是为单个程序中的单个执行而设计的


您需要连接自定义逻辑,将任务记录和随后的用户操作绑定在一起。最简单的方法是向用户的浏览器添加一个带有任务ID的cookie,以便文档上传控制器可以访问该ID,并更新系统的其余部分。在这里,您可以使用经典的Observer,这与Christoph的答案或internet上的任何其他示例非常相似。

有一些教程,您可以了解如何在PHP中实现Observer模式。你可以在这里阅读: