Fil*_*ans 5 php codeigniter observer-pattern
我正在经典 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
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以以 noobfriendly 的方式帮助我如何在 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();
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
344 次 |
| 最近记录: |