Hello,
I have an advanced file hierarchy.
- Application
- Backend
- Controller
- View
- Module.php
- Frontend
- ...
- Backend
- Component
- ...
- Geoloc
- City
- Controller
- Model
- View
- Module.php
- City
- View
- layout
- partial
All my routes redirect only on Backend or on Frontend.
I have build a City Component Service add to DI. I call it from a VilleController from Backend.
The Component should generates render of its own view and passes it to Backend Controller which generates rendering of its view with page layout and send to display. (Because there might be several Services called within same ControllerAction on Backend or Frontend)
I tried several versions of rendering but I can not connect the two parts.
PHP Fatal error: Call to undefined method AdCms\Component\Geoloc\Ville\Controller\IndexController::getId() in /.../AdCms/www/cache/volt/%%users%%elroliv%%sites%%adcms%%www%%app%%component%%geoloc%%ville%%view%%index%%villes.volt.php on line 8
getId() is a public function from my model and not from City Component IndexController.
Perhaps it's not the good way to render Component/Controller in its own view and pass html code at Interface/BackendController to render it's own view too.
Can someone advise me? Thank a lot
Component/Geoloc/City
File : Component/Geoloc/City/Module.php
$di->set('moduleView', function () {
$view = new Simple();
$view->setDI($this);
$view->setViewsDir(__DIR__ . '/View/');
$view->registerEngines([
'.volt' => 'volt',
'.phtml' => 'phpTemplating',
]);
return $view;
});
$di->setShared('ville', function () {
return new IndexController();
});
File : Component/Geoloc/City/Controller/IndexController.php
public function villesAction()
{
$template = $this->moduleView->render(
"index/villes",
[
"villes" => Ville::find(
[
"limit" => 100,
]
),
]
);
return $template;
}
File : Component/Geoloc/City/Model/Ville.php
class Ville extends Model
{
protected $id;
protected $name;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getCommune()
{
return $this->name;
}
public function setCommune($commune)
{
$this->name = $commune;
}
}
File : Component/Geoloc/City/View/index/villes.volt
<div class="page-header">
<h1>Module Geoloc / Ville</h1>
</div>
{% if not villes is empty %}
<h2>villes : </h2>
{% for ville in villes %}
{{ ville.getId() }} {{ ville.getName() }}{% if not loop.last %}<br>{% endif %}
{% endfor %}
{% else %}
<p><i>aucune ville récupérée</i></p>
{% endif %}
Application/Backend
File : Application/Backend/Module.php
$di->set('view', function () {
$view = new View();
$view->setDI($this);
$view->setViewsDir(array(__DIR__ . '/View/'));
$view->setLayoutsDir(APP_PATH . '/View/layout/');
$view->setTemplateAfter('bootstrap4-fixed-nav-dev');
$view->registerEngines([
'.volt' => 'volt',
'.phtml' => 'phpTemplating'
]);
return $view;
});
File : Application/Backend/Controller/VilleController.php
public function villeAction()
{
$villes = $this->getDI()->getShared('ville');
$this->view->villes = $villes->villesAction();
}
File : Application/Backend/View/ville/ville.volt
<div class="page-header">
<h1>Interface Backend / Ville</h1>
</div>
{{ villes }}