Scaffolding is used by our CRUD controllers. Thanks to this additional layer of abstraction you can switch between different databases adapter without changing your controller actions or attached events. Scaffolding should be registered as DI service, therefore it can be used outside the CRUD.
In the following example we will use scaffolding with event manager to extend basic CRUD functionality:
namespace Foo\Controllers\Foo;
use Phalcon\Forms\Element\Text;
use Vegas\Mvc\Controller\Crud\Events;
use Vegas\Mvc\Controller\Crud;
class FooController extends Crud
{
protected $formName = 'Foo\Forms\Foo';
protected $modelName = 'Foo\Models\Foo';
public function initialize()
{
parent::initialize();
$this->dispatcher->getEventsManager()->attach(Events::AFTER_CREATE, function () {
$this->changeFakeValue();
$this->redirect();
});
$this->dispatcher->getEventsManager()->attach(Events::AFTER_EDIT, function () {
$this->addExtraField();
});
}
// ...
private function changeFakeValue()
{
$record = $this->scaffolding->getRecord();
$record->fake = 'Fixed value';
$record->save();
}
private function addExtraField()
{
$form = $this->scaffolding->getForm($this->scaffolding->getRecord());
$field = new Text('extra_field');
$field->setLabel('Extra field');
$form->add($field);
$this->scaffolding->setForm($form);
}
public function redirect(){
$this->response->redirect(array(
'for' => 'foo/crud',
'action' => 'index',
));
}
}