Drupal:“错误:未找到类”,当从submitForm()中的控制器调用函数时,>> [

问题描述 投票:0回答:2
我正在尝试在自定义模块中提交表单时做一些事情。其中一些是通过从控制器调用函数来完成的。那就是我得到的:

Error: Class 'Drupal\ice_cream\Controller\OrderController' not found in Drupal\ice_cream\Form\OrderForm->submitForm() (line 77 of modules\custom\ice_cream\src\Form\OrderForm.php).

据我所知,命名空间没有错吗?还是与该错误无关?

这是我的OrderForm.php和SubmitForm()的样子:

<?php namespace Drupal\ice_cream\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\ice_cream\Controller\OrderController; /** * Implements the order form. */ class OrderForm extends FormBase { ... (omitted code for getFormid and buildForm) public function submitForm(array &$form, FormStateInterface $form_state) { //Check if the order is ice or waffles. if($form_state->getValue('foodType') == 'ice'){ //Save order to the DB. OrderController::saveOrder($form_state->getValue('foodType'), $form_state->getValue('taste')); ... (more code) } } }

这是控制器的外观:

<?php namespace Drupal\ice_cream\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Database\Database; /** * Order controller for interacting (insert, select,...) with the ice cream table in the DB. */ class OrderController extends ControllerBase { /** * Saves an order for either Ice or Waffles with their options (tast or toppings). */ public function saveOrder($foodType, $options) { $connection = Database::getConnection(); //Check if ice or waffles (to only insert the right field with $options). if($foodType == "ice"){ $result = $connection->insert('ice_cream') ->fields([ 'foodType' => $foodType, 'taste' => $options, 'toppings' => "", ]) ->execute(); return true; }elseif($foodType == "waffles"){ $result = $connection->insert('ice_cream') ->fields([ 'foodType' => $foodType, 'taste' => "", 'toppings' => $options, ]) ->execute(); return true; } } }

我正在尝试在自定义模块中提交表单时做一些事情。其中一些是通过从控制器调用函数来完成的。那是我得到的时间:错误:类'Drupal \ ice_cream \ Controller \ ...
drupal drupal-8
2个回答
0
投票
尝试使用下面的代码:

0
投票
已解决:
© www.soinside.com 2019 - 2024. All rights reserved.