Simple setting layout suggestion

Standard
<!DOCTYPE html>
<html>
    <head>
        {% block head %}
            <link rel="stylesheet" href="style.css" />
            <title>{% block title %}{% endblock %} - My Webpage</title>
        {% endblock %}
    </head>
    <body>
        <div id="content">{% block content %}{% endblock %}</div>
        <div id="footer">
            {% block footer %}
                &copy; Copyright 2011 by <a href="http://domain.invalid/">you</a>.
            {% endblock %}
        </div>
    </body>
</html>
Advertisements

Twig template engine

Standard

Twig in used by default in symfony

Operators

 

Thanks

Vinod

 

Form field adding rules

Standard

A dynamic form:

<?php

namespace Admin\ManageuserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(‘firstname’)
->add(‘lastname’)
->add(’email’)
->add(‘password’,’hidden’)
->add(‘status’, ‘choice’, array(‘choices’=>array(‘1’ => ‘Active’,’0′ => ‘Deactive’),
‘multiple’ => false,
‘expanded’ => true,
‘required’ => true,))

->add(’emailstatus’, ‘choice’, array(‘choices’=>array(‘1’ => ‘Active’,’0′ => ‘Deactive’),
‘multiple’ => false,
‘expanded’ => true,
‘required’ => true,))

;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
‘data_class’ => ‘Admin\ManageuserBundle\Entity\User’
));
}

public function getName()
{
return ‘admin_manageuserbundle_usertype’;
}
}

How to attach javascript and css code in layout

{% endblock %}</title>
{% block head %}
<link href=”{{ asset(‘bundles/framework/css/structure.css’) }}” rel=”stylesheet” type=”text/css” media=”all” />
<script language=”javascript” type=”text/javascript” src=”{{ asset(‘bundles/framework/js/js.js’) }}”></script>

{% endblock %}

 

how to add image

<img  src=”{{ asset(‘bundles/framework/images/logo_symfony.png’) }}”>

 

How to add Title

{% block title %} Symfony Test website

{% endblock %}

 

How to add header footer  for back end  in  twig file

{% extends ‘::base.html.twig’ %}
{% block body%}

data here

{% endblock %}

 

how to add  layout in twig file front

 

{% extends “TwigBundle::layout.html.twig” %}

{% block body -%}
front dat here

 

{% endblock %}

 

How to add select box in form

 

->add(‘organizationsize’, ‘choice’, array(
‘choices’ => array(
” => ‘Select – Organization Size…’,
‘500 Employees’ => ‘500 Employees’,
‘500 – 2000 Employees’ => ‘500 – 2000 Employees’,
‘2000 – 10000 Employees’ => ‘2000 – 10000 Employees’,
‘10000  Employees’ => ‘10000  Employees’
)))

 

 

How to add radio in form

->add(‘status’, ‘choice’, array(‘choices’=>array(‘1’ => ‘Active’,’0′ => ‘Deactive’),
‘multiple’ => false,
‘expanded’ => true,
‘required’ => true,))

 

 

How to manage session in twig file

 

{% if adminlogin is defined %}

code here

 

{% endif %}

 

front End

{% if userlogin|default is not empty %}

if session is availabe

{% endif %}

 

{% if userlogin|default is empty %}

if session not avialbel

{% endif %}

 

Layout content setting

This  block always up from footer section

{% block body %}{% endblock %}

 

CSS

{% block stylesheets %}

<link href=”{{ asset(‘bundles/framework/css/style.css’) }}” rel=”stylesheet” type=”text/css” media=”all” />

{% endblock %}

 

 

 

 

Google API Suggestion

Standard

How to get Google API Key – Google Maps API Key

Why you need Google API key ?

Google provides API to access their services. In order to use a Google service in your website, you need a API Key.
Google API Console: https://code.google.com/apis/console

List of service provided by Google?
Google Maps , Google Places, Google analytics, Google Drive, Google Books, Google + API, PageSpeed API, Youtube API, ULR Shortenter API, Google Cloud API, Google Android maps API, ..etc

Types of Google API Keys:
a). Browser apps API key
Browser key is used for services like Maps, where the API is used in user browser.
Example: Services with JavaScript ( Google Maps javascript API , URL Shoretener API )

b). Server apps API Key:
Server key is required for apps, where you use google API server-side.
Example: Services with PHP/Java/.NET (URL Shortener ,  Youtube Data API )

c). Authorized API key ( OAuth)
OAuth key is required, where the user authorization is required to access the service using the API.
Example: When you are making application to read mails from Gmail / To store files in Google Drive.

d).  Android API Key
Android key is required for android applications only.
Example: Google maps for android, Google Cloud Messaging.

Why you need to create a project ?
In order to create a API key, first you need to create a Project. A project can have multiple services and multiple API keys.
i.e: With the same API key, you can access multiple services provided by Google.

How to get Google API Key ?
Step1: Go to Google API Console: https://code.google.com/apis/console
Step 2: Create API Project
Step 3: Register Project ID ( Go to Project => Overview Tab)
Step 4: Select Services you want to use ( Go to Project => Services Tab )

For Browser apps API Key : By default, one browser key is created when you select some services. Otherwise (Go to Project => API Access Tab => Create new Browser Key button)
For Server apps API Key : Go to Project => API Access Tab => Create new Server key
For Android apps API Key : Go to Project => API Access Tab => Create new Android key
For OAuth API Key : Go to Project => API Access Tab => Create an OAuth 2.0 Client ID

 

 

 

Thanks

Vinod

Use symfony dynamic form.

Standard

Form

namespace Profile\RegisterBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(‘firstname’)
->add(‘lastname’)
->add(’email’)
->add(‘password’,’password’)
->add(‘status’,’hidden’,array(‘data’ => ‘0’,))
->add(’emailstatus’,’hidden’,array(‘data’ => ‘0’,))
;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
‘data_class’ => ‘Profile\RegisterBundle\Entity\User’
));
}

public function getName()
{
return ‘profile_registerbundle_usertype’;
}
}

View

{% extends “TwigBundle::layout.html.twig” %}

{% block body -%}

<form  align=”center” action=”{{ path(‘profile_register_homepage’) }}”    method=”post” {{ form_enctype(form) }}>
{% if msg is defined %}
{{msg}}
{%endif%}
{{form_widget(form) }}
<p>

<button align=”left” type=”submit” name=”submit”>submit</button>
</p>
</form>

{% endblock %}

Special Command for symfony 2.3 Framework development

Standard

I am sharing some special command with you…..

1) For create bundle

vinod@administrator:/var/www/myproject$ sudo app/console generate:bundle

2) For create Entity

vinod@administrator:/var/www/myproject$ sudo /usr/local/zend/bin/php app/console gen:doctrine:entity

3) For  create table in database

vinod@administrator:/var/www/myproject$ sudo /usr/local/zend/bin/php app/console doctrine:schema:update –force

4)How to create dynamic form

vinod@administrator:/var/www/myproject$ sudo /usr/local/zend/bin/php app/console generate:doctrine:form AdminCountryCountryBundle:Country

Here Country is your tablename that has been created by Entity.

5)  for dump the symfony table

vinod@administrator:/var/www/myproject$ sudo /usr/local/zend/bin/php app/console doctrine:schema:update –dump-sql

6)

For curd creation

vinod@administrator:/var/www/myproject$ sudo /usr/local/zend/bin/php app/console doctrine:generate:crud

Thanks

Vinod K Maurya(OSSCube Lead Engineer)

File uploading process in symfony2.3

Standard

Hi Friend i am sharing file uploading process in symfony 2.3 lot of thanks to ” Mr .Loic Favory”  Who help me some critical situation.

First you have to create a bundle :

vinod@administrator:/var/www/myproject$ sudo app/console generate:bundle

Then create Entity

vinod@administrator:/var/www/myproject$ sudo /usr/local/zend/bin/php app/console gen:doctrine:entity
Create step by step fieldname like

name , email, path

Then

vinod@administrator:/var/www/myproject$ sudo /usr/local/zend/bin/php app/console doctrine:schema:update –force

My bundle name is ManageGalleryBundle     you can make as per you requirement

Now i am sharing my code:

View Code index.html.twig

{% extends ‘::base.html.twig’ %}
{% block body -%}

<form action=”{{ path(‘admin_manage_gallery_homepage’) }}”    method=”post” {{ form_enctype(form) }}>

{% if msg is defined %}
{{msg}}
{%endif%}

{{ form_widget(form) }}
<p>
<button type=”submit” name=”submit”>upload</button>
</p>
</form>

{% endblock %}

Form :    UserfileType.php

<?php
namespace Admin\ManageGalleryBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserfileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(‘name’,’text’)
->add(’email’,’text’)
->add(‘file’,’file’)

;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
‘data_class’ => ‘Admin\ManageGalleryBundle\Entity\Userfile’
));
}

public function getName()
{
return ‘admin_manage_gallery_userfiletype’;
}

}

Controller Code

<?php
/* Added           : Vinod Kumar Maurya (Lead Engineer)
* Version        : Symfony 2.3
* Organization   : OSSCube Solutions Pvt Ltd
* Scope          : File uploading
* Dated          : 5th july 2013
*/

namespace Admin\ManageGalleryBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Admin\ManageGalleryBundle\Entity\Userfile;
use Admin\ManageGalleryBundle\Form\UserfileType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$session=$this->getRequest()->getSession();
$sessionid=$session->get(‘sessionid’);
$uploaddata  = new Userfile();
$request = $this->container->get(‘request’);
$form = $this->createForm(new UserfileType(), $uploaddata);
$form->handleRequest($request);
if($request->getMethod()==’POST’)
{

if ($form->isValid()) {
$em=$this->getDoctrine()->getEntityManager();
$files=$uploaddata->upload();
$em->persist($uploaddata);
$em->flush();
$msg=”File uploded Successfully”;
return $this->render(‘AdminManageGalleryBundle:Default:index.html.twig’, array(‘id’ => $uploaddata->getId(),’msg’=>$msg,’sessionid’=>$sessionid,’entity’=>$uploaddata,’form’=>$form->createView(),));
}
else

{
$msg=”File not uploded successfully”;
return $this->render(‘AdminManageGalleryBundle:Default:index.html.twig’, array(‘id’ => $uploaddata->getId(),’msg’=>$msg,’sessionid’=>$sessionid,’entity’=>$uploaddata,’form’=>$form->createView(),));
}
}
return $this->render(‘AdminManageGalleryBundle:Default:index.html.twig’,array(‘sessionid’=>$sessionid,’entity’=>$uploaddata,’form’=>$form->createView(),));

}

}

Entity : UserFile.php

<?php

namespace Admin\ManageGalleryBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;

/**
* Userfile
*/
class Userfile
{
/**
* @var integer
*/
private $id;

/**
* @var string
*/
private $name;

/**
* @var string
*/
private $email;

/**
* @var string
*/
private $path;

private $file;

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set name
*
* @param string $name
* @return Userfile
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Set email
*
* @param string $email
* @return Userfile
*/
public function setEmail($email)
{
$this->email = $email;

return $this;
}

/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}

/**
* Set path
*
* @param string $path
* @return Userfile
*/
public function setPath($path)
{
$this->path = $path;

return $this;
}

/**
* Get path
*
* @return string
*/
public function getPath()
{
$this->path;
}

public function getAbsolutePath()
{
return null === $this->path? null: $this->getUploadRootDir().’/’.$this->path;
}

public function getWebPath()
{
return null === $this->path? null: $this->getUploadDir().’/’.$this->path;
}

protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.’/../../../../web/’.$this->getUploadDir();
}

protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn’t screw up
// when displaying uploaded doc/image in the view.
return ‘uploads/documents’;
}

public function setFile($file = null)
{
$this->file = $file;
}

public function getFile()
{
return $this->file;
}

public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}

// use the original file name here but you should
// sanitize it at least to avoid any security issues

// move takes the target directory and then the
// target filename to move to
$this->getFile()->move(
$this->getUploadRootDir(),
$this->getFile()->getClientOriginalName()
);

// set the path property to the filename where you’ve saved the file
$this->path = $this->getFile()->getClientOriginalName();

// clean up the file property as you won’t need it anymore
$this->file = null;
}

}

Create Folder

web/uploads/documents and give 777 permission

 

table structure

Userfile

Field Type Null Default Comments
id int(11) No
name varchar(255) No
email varchar(255) No
path varchar(255) No

Indexes: Documentation

Keyname Type Unique Packed Field Cardinality Collation Null Comment
PRIMARY BTREE Yes No id 10 A