我正在经典CI mvc设置中构建应用程序,其中用户具有常规/通用任务列表。任务的主要目的是向用户指示他必须完成特定的操作,并将其重定向到他需要完成此操作的页面。
我的问题是,当用户被重定向到需要在其上进行操作的特定页面时,我们会丢失特定任务的上下文。因此,即使任务完成了(在本示例中,例如,文档已上传),任务本身也不知道,并且我们实际上没有连接来更新任务。
经过一些研究,观察者设计模式看起来可以满足这种需求。但是,通过所有示例,我都无法点击如何将其实际实现到当前系统中。
在处理文档上载的控制器中是函数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存储库,其中包含所请求语言的设计模式示例。对于PHP,我可以在这里热烈推荐这个:
https://designpatternsphp.readthedocs.io/en/latest/Behavioral/Observer/README.html
示例实现可能看起来像这样。注意:我没有使用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();
}
}
希望这会有所帮助。
如果我正确理解您的问题和意图(在原地创建的记录与其他地方发生的某些动作之间建立关系),那么观察者(如果我们指的是同一本书中的相同想法)就不会解决它,由于PHP和Web的一般无状态本质,即跨多个程序调用的执行,因此它甚至在这里也不适用。 GoF书中的经典模式旨在为单个程序中的单个执行而设计。
您需要连接自定义逻辑,以将任务记录和随后的用户操作绑定在一起。最简单的方法是将带有任务ID的cookie添加到用户的浏览器中,以便文档上传控制器可以访问该ID,并且它将更新系统的其余部分。您可以在那里使用经典的《观察者报》,就像克里斯多夫的回答或互联网上的任何其他示例一样。