Default Symfony controler Cheatsheet

namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
 
class HelloController
{
    public function indexAction($name)
    {
      return new Response('<html><body>Hello '.$name.'!</body></html>');
    }
}
 
 
//Param for route
//app/config/routing.yml
/* hello:
    pattern:      /hello/{first_name}/{last_name}
    defaults:     { _controller: AcmeHelloBundle:Hello:index, color: green }*/
 
// in the controler
public function indexAction($first_name, $last_name, $color)
{
    // ...
}
 
 
//The request
use Symfony\Component\HttpFoundation\Request;
 
public function updateAction(Request $request)
{
    $form = $this->createForm(...);
 
    $form->bindRequest($request);
    // ...
}
 
 
//Controler inheritance
namespace Acme\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
 
class HelloController extends Controller
{
    public function indexAction($name)
    {
      return new Response('<html><body>Hello '.$name.'!</body></html>');
    }
}
 
 
//Some courrent action
public function indexAction()
{
    //default sttus code, 302 (temp redirect)
    return $this->redirect($this->generateUrl('homepage'));
 
    return $this->redirect($this->generateUrl('homepage'), 301);
 
    //forward
    $response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
        'name'  => $name,
        'color' => 'green'
    ));
 
    //same as
    $httpKernel = $this->container->get('http_kernel');
    $response = $httpKernel->forward('AcmeHelloBundle:Hello:fancy', array(
        'name'  => $name,
        'color' => 'green',
    ));
 
    //template
    $content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
    return new Response($content);
 
    //same as
    return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
 
    //same as
    $templating = $this->get('templating');
    $content = $templating->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
 
    //important service
    $request = $this->getRequest();
    $templating = $this->get('templating');
    $router = $this->get('router');
    $mailer = $this->get('mailer');
 
    //throw exception
    $product = // retrieve the object from database
    if (!$product) {
        throw $this->createNotFoundException('The product does not exist');
    }
 
    return $this->render(...);
 
    //session
    $session = $this->getRequest()->getSession();
    $session->set('foo', 'bar');
    $foo = $session->get('foo');
    $session->setLocale('fr');
    //session, flash message
    $this->get('session')->setFlash('notice', 'Your changes were saved!');
 
/* In the template
{% if app.session.hasFlash('notice') %}
    <div class="flash-notice">
        {{ app.session.flash('notice') }}
    </div>
{% endif %}
*/
 
    //the response
    $response = new Response('Hello '.$name, 200);
 
    $response = new Response(json_encode(array('name' => $name)));
    $response->headers->set('Content-Type', 'application/json');
 
 
    //the request
    $request = $this->getRequest();
    $request->isXmlHttpRequest();
    $request->getPreferredLanguage(array('en', 'fr'));
    $request->query->get('page'); // get a $_GET parameter
    $request->request->get('page'); // get a $_POST parameter
 
    //routing
    //relative url
    $url = $this->get('router')->generate('blog_show', array('slug' => 'my-blog-post'));
    //absolute
    $router->generate('blog_show', array('slug' => 'my-blog-post'), true);
    //query string
    // /blog/2?category=Symfony
    $router->generate('blog', array('page' => 2, 'category' => 'Symfony'));
 
 
}
Processing your request, Please wait....

No Responses to “Default Symfony controler Cheatsheet”

Comments (Your Comments)

Leave a Reply

You must be logged in to post a comment.