有没有办法使用cakephp3在页面中调用无模式表单

Is there a way to call modeless form in pages using cakephp3

正如我在网上看到的那样,只有这样才能使用 http://localhost/xxxxx/contact 然后表格将显示

但我希望它显示在许多页面中,例如联系我们或关于我们页面

当我调用此页面时,我希望表单出现在内容中吗?

模板

index.ctp

<?= $this->Form->create($contact); ?>
<?= $this->Form->input('name'); ?>
<?= $this->Form->input('email'); ?>
<?= $this->Form->input('body'); ?>
<?= $this->Form->button('Submit'); ?>
<?= $this->Form->end(); ?>

ContactController.php

<?php
// In a controller
namespace App\Controller;

use App\Controller\AppController;
use App\Form\ContactForm;

class ContactController extends AppController
{
public function index()
{
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->data)) {
$this->Flash->success('Your message has been sent; we\'ll get back to you soon!');
$this->request->data['name'] = null;
$this->request->data['email'] = null;
$this->request->data['body'] = null;
} else {
$this->Flash->error('There was a problem submitting your form.');
}
}
$this->set('contact', $contact);
}
}

?>

ContactForm.php

<?php
namespace App\Form;

use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;
use Cake\Mailer\Email;

class ContactForm extends Form
{

protected function _buildSchema(Schema $schema)
{
return $schema->addField('name', 'string')
->addField('email', ['type' => 'string'])
->addField('body', ['type' => 'text']);
}

protected function _buildValidator(Validator $validator)
{
return $validator->add('name', 'length', [
'rule' => ['minLength', 10],
'message' => 'Please enter your name'
])->add('email', 'format', [
'rule' => 'email',
'message' => 'Please enter a valid email address',
])->add('body', 'length', [
'rule' => ['minLength', 25],
'message' => 'Please enter your message text',
]);
}

protected function _execute(array $data)
{
// Send an email.
    return true;
}
}

您可以通过将联系人模板表单移动到元素中来修复它,以便它在任何页面中都可用。

联系人文件夹中的内部元素,必须存在以下表格

<legend><?= __('Our Form') ?></legend>
    <fieldset>
        <?php
        echo $this->Form->input('name');
        echo $this->Form->input('email');
        echo $this->Form->input('body');
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end(); ?>

然后在您的页面中

你可以打电话

<?php 
   echo $this->element('contact/index');
 ?> 

假设您在 element

的联系人文件夹中创建了 index.ctp

希望它解决了您的问题。