我遇到了表单和POST方法的问题。 我在控制器中创建一个表单,如下所示:
$total =0;
function payableAmount($totalPrice){
// calculate total price here
global $total;
$total += $totalPrice;
echo $total;
}
我以这种方式在路由文件中配置“ ricerca_index ”:
$form = $this->createFormBuilder()
->setAction($this->generateUrl('ricerca_index'))
->setMethod('POST')
->add('search', 'text',array(
'label'=>'Cerca',
'required'=>false))
->add('submit', 'submit', array('label' => 'Cerca'))
->getForm();
$name = $request->getLocale();
return $this->render('DtEcBundle:Default:index.html.twig',array(
'form' => $form->createView()
在我的目标页面的控制器中,我用这种方式编写了在ricerca_index中打印我的变量:
################################################################################
# ricerca #
################################################################################
ricerca_index:
host: "{_locale}.{domain}"
locales: { it: "/ricerca.{_format}", fr: "/ricerca.{_format}", de: "/ricerca.{_format}" }
defaults: { _locale: "%locale%", domain: "%domain%", _format: "html", _controller: DtEcBundle:Search:index }
requirements:
_locale: "it|fr|de"
domain: "%domain%"
_format: "html|json"
method: "POST"
ricerca_index_www.it:
path: /ricerca.{_format}
host: "www.{domain}"
defaults: { _locale: "%locale%", _format: "html", _controller: DtEcBundle:Search:index }
requirements:
_locale: "%locale%"
domain: "%domain%"
_format: "html|json"
method: "POST"
ricerca_index_nowww.it:
path: /ricerca.{_format}
host: "{domain}"
defaults: { _locale: "%locale%", _format: "html", _controller: DtEcBundle:Search:index }
requirements:
_locale: "%locale%"
domain: "%domain%"
_format: "html|json"
method: "POST"
在twig文件中,我会打印我的变量形式“ textbox ”,然后我写下这段代码:
use Symfony\Component\HttpFoundation\Request;
class SearchController extends Controller {
public function indexAction(Request $request) {
$search = $request->get('search');
return $this->render('DtEcBundle:Search:index.html.twig',array(
"search" => $search,
));
}
我没有收到错误,但我的转储变量为null,问题在哪里?我是Symfony的新手。我怎样才能在页面中打印我的变量?
答案 0 :(得分:0)
就这样做:
$searchForm = $request->get('form');
$search = $searchForm['search'];
而不是$ request-> get('search');
您的帖子数据实际上是这样的:
[
'form' => [
'search' => <search_val>,
<...> // other values if any
]
]