The simplest controller with no extra actions and operations.
namespace Foo\Controllers\Frontend;
use Vegas\Mvc\Controller\ControllerAbstract;
class FooController extends ControllerAbstract
{
public function indexAction()
{
$this->view->foo = 'Vegas';
}
}
Utility methods:
$this->i18n->_()
. Works only if internationalization service is provided.Vegas\Mvc\Dispatcher\ExceptionResolver
.Vegas\Mvc\Dispatcher\ExceptionResolver
.Vegas\Mvc\Dispatcher\ExceptionResolver
.Provides you default CREATE, READ, UPDATE, DELETE operations.
namespace Foo\Controllers\Backend;
use Vegas\Mvc\Controller\Crud;
use Vegas\Mvc\Controller\Crud\Events;
class FooController extends Crud
{
protected $formName = 'Foo\Forms\Bar'; //Required. It will be displayed in add.volt and edit.volt template.
protected $modelName = 'Foo\Models\Bar'; //Required. It will be used to create, refresh, update, delete items.
public function initialize()
{
parent::initialize();
//Attach some events here
$this->dispatcher->getEventsManager()->attach(Events::AFTER_SAVE, function() {
//Redirect to index.volt
});
}
public function indexAction()
{
//List of records
}
}
Flow and available events for CRUD actions:
beforeNew()
(Alias: Vegas\Mvc\Controller\Crud\Events::BEFORE_NEW) - is triggered before reading the form to the viewafterNew()
(Alias: Vegas\Mvc\Controller\Crud\Events::AFTER_NEW) - is triggered after reading the form to the viewFoo\views\backend\foo\new.volt
view is loadedbeforeCreate
(Alias: Vegas\Mvc\Controller\Crud\Events::BEFORE_CREATE) - is triggered before any instruction in actionbeforeSave
(Alias: Vegas\Mvc\Controller\Crud\Events::BEFORE_SAVE) - is triggered before create operation (and also before update operation in updateAction())afterSave
(Alias: Vegas\Mvc\Controller\Crud\Events::AFTER_SAVE) - is triggered after create operation (and also after update operation in updateAction())afterCreate
(Alias: Vegas\Mvc\Controller\Crud\Events::AFTER_CREATE) - is triggered after create operationVegas\Mvc\Controller\Crud\Events::AFTER_CREATE_EXCEPTION
- is triggered when any exception is thrown during create operationbeforeEdit()
(Alias: Vegas\Mvc\Controller\Crud\Events::BEFORE_EDIT) - is triggered before any instruction in actionafterRead()
(Alias: Vegas\Mvc\Controller\Crud\Events::AFTER_READ) - is triggered after reading record from database but before binding record with formafterEdit()
(Alias: Vegas\Mvc\Controller\Crud\Events::AFTER_EDIT) - is triggered after binding record with formFoo\views\backend\foo\edit.volt
view is loadedbeforeUpdate()
(Alias: Vegas\Mvc\Controller\Crud\Events::BEFORE_UPDATE) - is triggered before any instruction in actionbeforeSave()
(Alias: Vegas\Mvc\Controller\Crud\Events::BEFORE_SAVE) - is triggered before update operation (and also before create operation in createAction())afterSave()
(Alias: Vegas\Mvc\Controller\Crud\Events::AFTER_SAVE) - is triggered after update operation (and also after insert operation in createAction())afterUpdate()
(Alias: Vegas\Mvc\Controller\Crud\Events::AFTER_UPDATE - is triggered after update operationVegas\Mvc\Controller\Crud\Events::AFTER_UPDATE_EXCEPTION
- is triggered when any exception is thrown during update operationbeforeDelete()
(Alias: Vegas\Mvc\Controller\Crud\Events::BEFORE_DELETE) - is triggered before delete operationafterDelete()
(Alias: Vegas\Mvc\Controller\Crud\Events::AFTER_DELETE) - is triggered after delete operation
|
|
Sample routing saved in Foo\config\routing.php
:
return array(
'foo' => array(
'route' => '/foo/:action/:params',
'paths' => array(
'module' => 'Foo',
'controller' => 'Backend\Foo',
'action' => 1,
'params' => 2
)
)
);
Required views you must have in Foo\views\backend\foo
directory:
index.volt
- should list items loaded and assigned to the view variable in indexAction()new.volt
- should display form to create record
<form action="{{ url.get(['for':'foo/crud', 'action':'create']) }}" method="post">
{{ partial('backend/foo/_form', ['form': form]) }}
</form>
edit.volt
- should display form to edit record
<form action="{{ url.get(['for':'foo/crud', 'action':'update', 'params': record._id]) }}" method="post">
{{ partial('backend/foo/_form', ['form': form]) }}
</form>
_form.volt
- form html
{% for element in form %}
{% do element.setAttribute('class', element.getAttribute('class')~' form-control') %}
{% set hasErrors = form.hasMessagesFor(element.getName()) %}
<div class="clearfix form-group{% if hasErrors %} has-error{% endif %}">
<label for="{{ element.getName() }}">{{ element.getLabel() }}</label>
{% if hasErrors %}
<span class="help-block">
{% for error in form.getMessagesFor(element.getName()) %}
{{ error }}
{% endfor %}
</span>
{% endif %}
{{ element }}
</div>
{% endfor %}
<div class="form-group">
<button type="submit" class="btn btn-flat success">Save</button>
<a href="{{ url.get(['for':'foo/crud', 'action':'index']) }}" class="btn pull-right">Cancel</a>
</div>
Provides you all CRUD operations and UPLOAD. To use it you must make sure you have uploader in DI. Read more.
namespace Category\Controllers\Backend;
use Vegas\Mvc\Controller;
class CategoryController extends Controller\CrudUpload
{
protected $formName = 'Foo\Forms\Bar'; //Required. It will be displayed in add.volt and edit.volt template.
protected $modelName = 'Foo\Models\Bar'; //Required. It will be used to create, refresh, update, delete items.
public function initialize()
{
parent::initialize();
//Attach some events here
$this->dispatcher->getEventsManager()->attach(Events::AFTER_SAVE, function() {
//Redirect to index.volt
});
}
public function indexAction()
{
//List of records
}
}