4

I am learning symfony2 and i have created one form in controller which is as bellow. the controller file as DefaultController.php

namespace Banner\TestBundle\Controller;

use Banner\TestBundle\Entity\Contact;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Banner\TestBundle\Entity\Task;
use Symfony\Components\HttpFoundation\Request;

class DefaultController extends Controller
{
public function newAction(Request $request)
{

 echo "The controller is called ";
 $task = new Task();
 $task->setTask("Write a blog Post ");
 $task->setDueDate(new DateTime('tomorrow'));

 $form =  $this->createFormBuilder($task)
            ->add('task','text  ')
            ->add('duedate','date')
            ->getForm();
    return $this->render('BannerTestBundle:default:zoo.html.twig',array('form'=>$form->createView()));

}
}

my routing file is as below. routing.yml

task_new:
pattern:  /task/{request}
defaults: { _controller: BannerTestBundle:Default:new}

and the zoo.html.twig file is as bellow.

{% extends '::page.html.twig' %}
    {% block title %}The Zoo{% endblock %}

    {% block content %}

        <form action="{{ path('task_new') }}" method="post" {{ form_enctype(form) }}>
                {{ form_widget(form) }}
            <input type="submit">   
        </form> 

    {% endblock %}

as i am passing the "task/GET" in my url it will shows the 'Request does not exist. error 500'.

what i basically want to do is when i pass the url the zoo.html.twig will be called. i want to display the form in zoo.html.twig.

2 Answers 2

6

You don't need to pass $request to your action and you don't need to put it in your route. The request object is available in global context. So

task_new:
    pattern:  /task-new
    defaults: { _controller: BannerTestBundle:Default:new}

You can either leave it this way

public function newAction(Request $request)

or have

public function newAction()

and access request object like this

$request  = $this->getRequest();
Sign up to request clarification or add additional context in comments.

Comments

1

Your routing file is not properly indented so the yaml will not parse properly. Try:

task_new:
    pattern:  /task/{request}
    defaults: { _controller: BannerTestBundle:Default:new}

Also:

  1. It is probably a bad idea to override request that way
  2. You don't appear to be doing anything with request in your controller

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.