... //set routers $di->set('router', function() { $router = new \Phalcon\Mvc\Router\Annotations(false); $router->removeExtraSlashes(true); $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); $router->addResource('Index', "/"); $router->notFound([ "controller" => "index", "action" => "page404" ]); return $router; }); ...
... /** * @Post("create") */ public function createAction() { /... } ...
... //Set a models manager $di->set('modelsManager', new \Phalcon\Mvc\Model\Manager()); //Set the models cache service $di->set('modelsCache', function() { //Cache data for one day by default $frontCache = new \Phalcon\Cache\Frontend\Data([ "lifetime" => 86400 ]); $cache = new \Phalcon\Cache\Backend\Memcache($frontCache, [ "host" => "localhost", "port" => "11211", 'persistent' => TRUE, ]); return $cache; }); $di->set('modelsMetadata', function() { // Create a meta-data manager with APC //$metaData = new \Phalcon\Mvc\Model\MetaData\Apc([ // "lifetime" => 86400, // "prefix" => "general-phsql" //]); $metaData = new \Phalcon\Mvc\Model\MetaData\Memory([ 'prefix' => 'general', ]); $metaData->setStrategy(new StrategyAnnotations()); return $metaData; }); ...
<?php namespace Frontend\Model; class Users extends \Phalcon\Mvc\Model { /** * @Primary * @Identity * @Column(type="integer", nullable=false) * @FormOptions(type=hidden) */ public $id; /** * @Column(type="string", nullable=false) * @FormOptions(type=text, length=32) */ public $name; /** * @Column(type="integer", nullable=false) * @FormOptions(type=email) */ public $email; /** * @Column(type="integer", nullable=false) * @FormOptions(type=text, length=9, pattern='[0-9]{9}') */ public $indcode; }
... //Annotations $di->set('annotations', function() { return new \Phalcon\Annotations\Adapter\Memory(); }); ...
<?php use Phalcon\Forms\Form, \Phalcon\Forms\Element\Submit as Submit; class EntityForm extends Form { public $fields = []; private $classprefix = '\\Phalcon\\Forms\\Element\\'; public $action; /** * @param object $model, action */ public function initialize($model, $action) { $this->action = $action; // $object = $model; $this->setEntity($object); // $attributes = $this->modelsMetadata->getAttributes($object); // $metadata = $this->annotations->get($object); // @FormOptions foreach ( $attributes as $attribute ) { $this->fields[$attribute] = $metadata ->getPropertiesAnnotations()[$attribute] ->get('FormOptions') ->getArguments(); } // foreach ($this->fields as $field => $type) { $fieldtype = array_shift($type); // type $fieldclass = $this->classprefix.$fieldtype; $this->add(new $fieldclass($field, $type)); // label if ( $fieldtype !== 'hidden') { $this->get($field)->setLabel($this->get($field)->getName()); } } // $this->add(new Submit('submit',[ 'value' => 'Send', ])); } public function renderform() { echo $this->tag->form([ $this->action, 'id' => 'actorform', ]); //fill form tags foreach ($this as $element) { // collect messages $messages = $this->getMessagesFor($element->getName()); if (count($messages)) { // each element render echo '<div class="messages">'; foreach ($messages as $message) { echo $message; } echo '</div>'; } echo '<div>'; echo '<label for="', $element->getName(), '">', $element->getLabel(), '</label>'; echo $element; echo '</div>'; } echo $this->tag->endForm(); } }
... /** * @Get("form") */ public function formAction() { $myform = new EntityForm(new Users(), 'create'); $this->view->setVars([ 'myform' => $myform, ]); } ...
... /** * @Post("create") */ public function createAction() { echo '<pre>'; var_dump($_POST); echo '</pre>'; } ...
{{ myform.renderform() }}
<?php use Phalcon\Mvc\View\Engine\Volt; use Phalcon\Mvc\Model\MetaData\Strategy\Annotations as StrategyAnnotations; try { //Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerDirs([ '../app/controllers/', '../app/models/', '../app/forms/' ]); $loader->registerNamespaces([ 'Frontend\\Model' => __DIR__.'/../app/models/', ]); $loader->register(); //Create a DI $di = new \Phalcon\DI\FactoryDefault(); //Set a models manager $di->set('modelsManager', new \Phalcon\Mvc\Model\Manager()); //Set the models cache service $di->set('modelsCache', function() { //Cache data for one day by default $frontCache = new \Phalcon\Cache\Frontend\Data([ "lifetime" => 86400 ]); $cache = new \Phalcon\Cache\Backend\Memcache($frontCache, [ "host" => "localhost", "port" => "11211", 'persistent' => TRUE, ]); return $cache; }); $di->set('modelsMetadata', function() { // Create a meta-data manager with APC //$metaData = new \Phalcon\Mvc\Model\MetaData\Apc([ // "lifetime" => 86400, // "prefix" => "general-phsql" //]); $metaData = new \Phalcon\Mvc\Model\MetaData\Memory([ 'prefix' => 'general', ]); $metaData->setStrategy(new StrategyAnnotations()); return $metaData; }); //SQL profiler $di->set('profiler', function(){ return new \Phalcon\Db\Profiler(); }, true); //set database connection $di->set('db', function() use ($di) { $eventsManager = new \Phalcon\Events\Manager(); //Get a shared instance of the DbProfiler $profiler = $di->getProfiler(); //Listen all the database events $eventsManager->attach('db', function($event, $connection) use ($profiler) { if ($event->getType() == 'beforeQuery') { $profiler->startProfile($connection->getSQLStatement()); } if ($event->getType() == 'afterQuery') { $profiler->stopProfile(); } }); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql([ "host" => "localhost", "username" => "root", "password" => "12345", "dbname" => "general" ]); //Assign the eventsManager to the db adapter instance $connection->setEventsManager($eventsManager); return $connection; }); //Register Volt as a service $di->set('voltService', function($view, $di) { $volt = new Volt($view, $di); $volt->setOptions([ "compiledPath" => "../app/cache/", ]); return $volt; }); //Setting up the view component $di->set('view', function(){ $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); $view->registerEngines([ ".volt" => 'voltService' ]); return $view; }); //Create Form manager $di->set('forms', function() { $forms = new \Phalcon\Forms\Manager(); return $forms; }); $di->set('session', function() use($di) { $session = new Phalcon\Session\Adapter\Files(); $session->setoptions([ 'uniqueId' => 'privatRsc', ]); $session->start(); return $session; }); //set routers $di->set('router', function() { $router = new \Phalcon\Mvc\Router\Annotations(false); $router->removeExtraSlashes(true); $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); $router->addResource('Index', "/"); $router->notFound([ "controller" => "index", "action" => "page404" ]); return $router; }); //Annotations $di->set('annotations', function() { return new \Phalcon\Annotations\Adapter\Memory(); }); //Handle the request $application = new \Phalcon\Mvc\Application($di); echo $application->handle()->getContent(); } catch(\Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage(); }
<?php use \Frontend\Model\Users as Users; /** * @RoutePrefix("/") **/ class IndexController extends \Phalcon\Mvc\Controller { /** * @Get("") */ public function indexAction() { echo <h3>Index Action</h3>; } /** * @Get("form") */ public function formAction() { $myform = new EntityForm(new Users(), 'create'); $this->view->setVars([ 'myform' => $myform, ]); } /** * @Post("create") */ public function createAction() { echo '<pre>'; var_dump($_POST); echo '</pre>'; } /** * @Get("page404") */ public function page404Action() { echo '404 - route not found'; } }
<?php use Phalcon\Forms\Form, \Phalcon\Forms\Element\Submit as Submit; class EntityForm extends Form { public $fields = []; private $classprefix = '\\Phalcon\\Forms\\Element\\'; public $action; /** * @param object $model, action */ public function initialize($model, $action) { $this->action = $action; //Set fields options from annotations $object = $model; $this->setEntity($object); $attributes = $this->modelsMetadata->getAttributes($object); $metadata = $this->annotations->get($object); foreach ( $attributes as $attribute ) { $this->fields[$attribute] = $metadata ->getPropertiesAnnotations()[$attribute] ->get('FormOptions') ->getArguments(); } foreach ($this->fields as $field => $type) { $fieldtype = array_shift($type); $fieldclass = $this->classprefix.$fieldtype; $this->add(new $fieldclass($field, $type)); if ( $fieldtype !== 'hidden') { $this->get($field)->setLabel($this->get($field)->getName()); } } $this->add(new Submit('submit',[ 'value' => 'Send', ])); } public function renderform() { echo $this->tag->form([ $this->action, 'id' => 'actorform', ]); //fill form tags foreach ($this as $element) { // collect messages $messages = $this->getMessagesFor($element->getName()); if (count($messages)) { // each element render echo '<div class="messages">'; foreach ($messages as $message) { echo $message; } echo '</div>'; } echo '<div>'; echo '<label for="', $element->getName(), '">', $element->getLabel(), '</label>'; echo $element; echo '</div>'; } echo $this->tag->endForm(); } }
<?php namespace Frontend\Model; class Users extends \Phalcon\Mvc\Model { /** * @Primary * @Identity * @Column(type="integer", nullable=false) * @FormOptions(type=hidden) */ public $id; /** * @Column(type="string", nullable=false) * @FormOptions(type=text, length=32) */ public $name; /** * @Column(type="integer", nullable=false) * @FormOptions(type=email) */ public $email; /** * @Column(type="integer", nullable=false) * @FormOptions(type=text, length=9, pattern='[0-9]{9}') */ public $indcode; }
<h2>Test form in Volt</h2> <hr> {{ myform.renderform() }} <hr>
Source: https://habr.com/ru/post/197254/
All Articles