Symfony2 Routing using Annotation


kita akan Edit kode yang sebelumnya kita buat disini (kita tambah beberapa anotasi dan satu fungsi baru) :

https://undebugable.wordpress.com/2015/09/07/hello-world-from-new-bundle/

menjadi seperti  disini :

<?php

///var/www/html/learning/learning1/src/Acme/TestBundle/Controller/HoiWorldController.php
namespace Acme\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

class HoiWorldController extends Controller {

/**
* @Route(“/hoi/{msg}”, name=”acme_hoi”)
*/
public function greatingAction($msg)
{

$html = $this->container
->get(‘templating’)
->render(
‘AcmeTestBundle:Hoi:hoi.html.twig’,
array(‘msg’ => $msg)
);

return new Response($html);
}

/**
* @Route(“/hoi2/{msg}”, name=”acme_hoi2″)
*/
public function greating2Action($msg)
{

return $this->render(‘AcmeTestBundle:Hoi:hoi.html.twig’, array(‘msg’ => $msg) );
}

/**
* Hanya redirect / Just redirect to acme_hoi2
* @Route(“/hoi3/{msg}”, name=”acme_hoi3″)
*/
public function greating3Action($msg)
{
return $this->redirectToRoute(‘acme_hoi2’, [‘msg’ => $msg ] ); // > 2.6 only
}

}

Dari kode diatas kita punya URL Routes seperti berikut :

  1. http://127.0.0.1:8000/hoi/hello%20world
  2. http://127.0.0.1:8000/hoi2/hello%20world
  3. http://127.0.0.1:8000/hoi3/hello%20world     <- ini redirect ke no 2

Nah skarang mau kita rubah ada nama bundlenya misal jadi begini :

  1. http://127.0.0.1:8000/acme/hoi/hello%20world
  2. http://127.0.0.1:8000/acme/hoi2/hello%20world
  3. http://127.0.0.1:8000/acme/hoi3/hello%20world     <- ini redirect ke no 2

maka tmbahkan saja anotasi @Route di Class nya

<?php
namespace Acme\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
* @Route(“/acme”)
*/
class HoiWorldController extends Controller {

/**
* @Route(“/hoi/{msg}”, name=”acme_hoi”)
*/
public function greatingAction($msg)
{

$html = $this->container
->get(‘templating’)
->render(
‘AcmeTestBundle:Hoi:hoi.html.twig’,
array(‘msg’ => $msg)
);

return new Response($html);
}

/**
* @Route(“/hoi2/{msg}”, name=”acme_hoi2″)
*/
public function greating2Action($msg)
{

return $this->render(‘AcmeTestBundle:Hoi:hoi.html.twig’, array(‘msg’ => $msg) );
}

/**
* Hanya redirect / Just redirect to acme_hoi2
* @Route(“/hoi3/{msg}”, name=”acme_hoi3″)
*/
public function greating3Action($msg)
{
return $this->redirectToRoute(‘acme_hoi2’, [‘msg’ => $msg ] ); // > 2.6 only
}

}

Kembali ke daftar isi

One thought on “Symfony2 Routing using Annotation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s