Hi there,
I’m using a page model (below) to create virtual pages. I create pages by fetching a .xml file with some data. I’m using Data::read() to read the .xml file. I want to catch the error message if this steps fails and then show a message on page render. Is there a way I can pass/return $error to the controller or the page directly but also return the other “working” pages at the same time? Like what I would usually do inside a controller?
I’d like to inform the user that one of the “scanned” files could not be read but still return all working virtual pages if that makes sense.
<?php
use Kirby\Data\Data;
use Kirby\Filesystem\F;
class HomePage extends Page
{
public function children()
{
if ($this->children instanceof Pages) {
return $this->children;
}
$naslist = [];
$pages = [];
$basepath = option('nas.root.path');
$error = null;
/**
* Scan $basepath and build an array of all subdirectories.
* Each subdirectory represends a NAS
*/
$naslist = array_values(array_diff(scandir($basepath), array('..', '.')));
/**
* Loop through all detected subdirectories
* and create virtual pages for each NAS
*/
foreach ($naslist as $key => $nas) {
$path = $basepath . $nas;
$xmlfile = $path . '/' . $nas . '.xml';
// Let's make sure that this NAS has the {NAS_NAME}.xml file
if(F::exists($xmlfile)) {
try {
$xml = Data::read($xmlfile);
$pages[] = [
'slug' => Str::slug($nas),
'num' => $key+1,
'template' => 'nas',
'model' => 'nas',
'content' => [
'title' => $nas,
...
]
];
} catch (Exception $e) {
$error = $e->getMessage();
}
}
}
return Pages::factory($pages, $this);
}
}