Should the server or network shares be accessed via the controller directly or some kind of model created?
I can use code like this in the controller:
$path = '/var/www/uploads';
$fileArray = array();
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$fileArray[] = array('filename' => $fileinfo->getFilename(), 'extension' => $fileinfo->getExtension(), 'size' => $this->bytesToSize($fileinfo->getSize(),2), 'modified' => date('d/m/Y H:i', $fileinfo->getMTime()));
}
}
$this->view->fileList = $fileArray;
This would mean repeating every time which seems the wrong way to do things in MVC. Should I be creating a model that maps the file system, or does Zend have domething that can do this?
e.g. //PSEUDO CODE
$directory = new Zend_Directory();
$this->view->filelist = $directory->listFiles();
$directory->addFile('filepath');
$directory->deleteFile('filepath');
With similar sorts of functionality, upload, delete, rename, move, copy, etc.
Any ideas?
Make it a model. That way you can do unit tests, easily swap out new controllers, and reuse the model in other projects.
A good rule of thumb is: If it directly interacts with data (a database, a service, a file, etc.), make it a model.
And there actually is a proposal for
Zend_DirectoryandZend_File(link), but it doesn’t look like any work has been done on it since 2009.You might want to consider packaging your code as a plugin that others can use. Maybe use the link I posted above as a starting point.