Monday, May 4, 2015

Symfony

Step 1
Set the path to project folder in the command prompt

Step 2
Type the following command
            Php app/console server:run

Step 3
Create Entity folder in the demobundle à project/project1/src/Acme/DemoBundle
In the Entity folder, create new file à Person.php ( contains attributes, asserts, getter and setters )

Person.php


<?php
namespace Acme\DemoBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;

class Person
{

 /**
  *@Assert\NotBlank()
  *@Assert\Email()
 */
 protected $email;

 /**
  *@Assert\NotBlank()
 */
 protected $fullname;


 public function getEmail()
 {
  return $this -> email;
 }
 public function setEmail($email)
 {
  $this-> email = $email;
 }
 public function getFullname()
 {
  return $this->fullname;
 }
 public function setFullname($fullname)
 {
  $this-> fullname = $fullname;
 }
}

?>


Step 4

Create php file in Acme/Demobundle/Form as PersonType.php.
In here we create the form to be appeared on the browser


PersonType.php


<?php
Namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;


class PersonType extends AbstractType
{

 Public function buildForm(FormBuilderInterface $builder, array $options)
 {
  $builder -> add('email','email')->add('fullname','text')->add('submit','submit');
 }

 Public function getName()
 {
  return 'person';
 }
}






Step 5

We modify the twig.
Demobundle à resources à views à welcome folder à index.html.twig

Remove all the <div> blocks and contents within them.


Index.html.twig

{% extends 'AcmeDemoBundle::layout.html.twig' %}

{% block title %}Symfony - Welcome{% endblock %}

{% block content_header '' %}

{% block content %}
    {% set version = constant('Symfony\\Component\\HttpKernel\\Kernel::MAJOR_VERSION') ~ '.' ~ constant('Symfony\\Component\\HttpKernel\\Kernel::MINOR_VERSION')%}

    <h1 class="title">Welcome!</h1>

    <p>Congratulations! You have successfully installed a new Symfony application.</p>
 
 {{form_start(form , {attr : {novalidate:'novalidate'}})}}
 {{form_row(form.email)}}
 {{form_row(form.fullname)}}
 {{form_end(form)}}
 
{% endblock %}


Step 6

We modify the WelcomeController
Demobundle à controller à WelcomeController



welcomController.php


<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Acme\DemoBundle\Entity\Person;
use Acme\DemoBundle\Form\PersonType;
use Symfony\Component\HttpFoundation\Response;


class WelcomeController extends Controller
{
    public function indexAction()
    {
        /*
         * The action's view can be rendered using render() method
         * or @Template annotation as demonstrated in DemoController.
         *
         */
   
   //$user=array('name'=>'Nimal','active'=>true);
   
   $person = new Person();
   $form = $this->createForm(new PersonType(),$person);
   
   
  $request = $this->get('request');
  $form-> handleRequest($request);
  if($request->getMethod() == 'POST')
  {
   if($form -> isValid())
   {
    return new Response('Person Created');
   }
   else
   {
    return new Response('Error Occured while creating Person');
   }
   
  
   //return $this->render(‘AcmeDemoBundle:Main:index.html.twig’, array[‘form’=>$form=>createView()]);
   

  
  }
 
  return $this->render('AcmeDemoBundle:Welcome:index.html.twig' , array('form'=>$form->createView()));

 }
}





No comments:

Post a Comment