Hello World from new Bundle


Membuat Hello World di bundle baru Symfony2

Baca yang sebelumnya tentang create bundle disini :

Symfony Bundle System (link)

dari artikel di atas kan sekaran kita memiliki bundle baru (fresh) yang bernama:

AcmeTestBundle

nah sekarang kita mau buat hello world disini (bundle ini)

Buat Controller baru di bundle baru tersebut (HoiWorldController.php) :

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

Isi dari HoiWorldController.php :

<?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}”)
*/
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}”)
*/
public function greating2Action($msg)
{

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

}

buat template buat view nya :

/var/www/html/learning/learning1/src/Acme/TestBundle/Resources/views/Hoi/hoi.html.twig

isinya

{% extends ‘base.html.twig’ %}

{% block body %}
<h1>From Acme: {{msg}}</h1>
{% endblock %}

nah sekarang bisa akses http://127.0.0.1:8000/hoi/hello%20world dan http://127.0.0.1:8000/hoi2/hello%20world

2 thoughts on “Hello World from new Bundle

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