我要为此创建一个调查表,所以我可以通过一种方法Web链接手动创建一个控制器,该方法使用表单列出屏幕上的所有问题。
当我单击“提交”按钮时,我从请求数据中调用同一控制器中的添加方法,但它给出了错误。
<?php
namespace App\Controller;
use App\Controller\AppController;
/**
* Users Controller
*
* @property \App\Model\Table\UsersTable $Users
*
* @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface
paginate($object = null, array $settings = [])
*/
class SurveyquestionController extends AppController
{
public function beforeFilter($event)
{
parent::beforeFilter($event);
// Allow users to register and logout.
// You should not add the "login" action to allow list. Doing so would
// cause problems with normal functioning of AuthComponent.
$this->Auth->allow(['webink']);
}
/**
* webink method
*
* @return \Cake\Http\Response|void
*/
public function webink()
{
$this->loadModel ('Questionmaster');
$questions = $this->Questionmaster->find('all')->contain(['Questiontype']);
$this->set(compact('questions'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
print_r($this->request->getData());
die;
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'login']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$this->set(compact('user'));
}
}
AppController
<?php
/ ** * CakePHP(tm):快速开发框架(https://cakephp.org) *版权所有(c)Cake Software Foundation,Inc.(https://cakefoundation.org) * *根据MIT许可获得许可 *有关完整的版权和许可信息,请参阅LICENSE.txt *文件的重新分发必须保留上述版权声明。 * * @copyright版权所有(c)Cake Software Foundation,Inc.(https://cakefoundation.org) * @link https://cakephp.org CakePHP(tm)项目 * @自0.2.9起 * @license https://opensource.org/licenses/mit-license.php MIT许可证 * / 命名空间App \ Controller;
使用Cake \ Controller \ Controller; 使用Cake \ Event \ Event;
/ ** *应用控制器 * *在下面的类中添加您的应用程序级方法,即您的控制器 *将继承它们。 * * @link https://book.cakephp.org/3.0/en/controllers.html#the-app-controller * / AppController类扩展Controller {
/**
* Initialization hook method.
*
* Use this method to add common initialization code like loading components.
*
* e.g. `$this->loadComponent('Security');`
*
* @return void
*/
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler', [
'enableBeforeRedirect' => false,
]);
$this->loadComponent('Flash');
$this->loadComponent('Csrf');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'username',
'password' => 'password'
]
]
],
'loginRedirect' => [
'controller' => 'Survey',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'users',
'action' => 'login'
],
//use isAuthorized in Controllers
'authorize' => 'Controller',
'unauthorizedRedirect' => $this->referer()
]);
/*
* Enable the following component for recommended CakePHP security settings.
* see https://book.cakephp.org/3.0/en/controllers/components/security.html
*/
//$this->loadComponent('Security');
}
public function beforeFilter($event) {
parent::beforeFilter($event);
// Allow users to register and logout.
$this->Auth->allow(['login', 'logout']);
}
}