Friday, November 1, 2013

simple web service zend framework 2 | zf2

Hi all, Today I am going to code simple web service from ZF2.

As first step, you need to set up zf2 . Follow the instruction given in following link

http://framework.zend.com/manual/2.0/en/user-guide/skeleton-application.html

Mysoap is folder name of my new module. and it's place under root module on ZF2.

Copy all the folder from Application and past into Mysoap. Now we are ready to develop  our web service. see the below for folder structure


1.
C:\xampp\htdocs\ZendFramework\module\Mysoap\config\module.config.php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Mysoap;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                   
                ),
            ),
        );
    }
}


2.C:\xampp\htdocs\ZendFramework\module\Mysoap\src\Mysoap\Controller\IndexController.php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Mysoap\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;


use Zend\Soap\Client;
use Zend\Soap\Server;
use Zend\Soap\AutoDiscover;


require_once 'C:\xampp\htdocs\ZendFramework\module\Mysoap\src\Services\servicesAPI.php';



class IndexController extends AbstractActionController
{



    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {

        return new ViewModel();
    }

    public function soapAction()
    {

        // initialize server and set URI
        $server = new Server(null,
            array('uri' => 'http://myzend.local/mysoap/wsdl'));

        // set SOAP service class
        $server->setClass('servicesAPI');

        // handle request
        $server->handle();

        return $this->getResponse();
    }

    public function wsdlAction()
    {


        // set up WSDL auto-discovery
        $wsdl = new AutoDiscover();

        // attach SOAP service class
        $wsdl->setClass('servicesAPI');

        // set SOAP action URI
        $wsdl->setUri('http://myzend.local/mysoap/soap');

        // handle request
        $wsdl->handle();
        return $this->getResponse();
    }

    public function clientAction()
    {
        $url = 'http://myzend.local/mysoap/wsdl';

        $client = new Client($url);

        print_r($client->getProducts());


        return $this->getResponse();
    }


}

3. C:\xampp\htdocs\ZendFramework\module\Mysoap\config\module.config.php

return array(
    'controllers' => array(
        'invokables' => array(
            'Mysoap\Controller\Index' => 'Mysoap\Controller\IndexController',
        ),
    ),

    // The following section is new and should be added to your file
    'router' => array(
        'routes' => array(
            'album' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/mysoap[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Mysoap\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);

4. create new folder call Service under SRC. This our web service code
C:\xampp\htdocs\ZendFramework\module\Mysoap\src\Services\servicesAPI.php


class servicesAPI {
    /**
     * This method returns a string
     *
     * @param String $value
     * @return String
     */
    // define filters and validators for input
    private $_filters = array(
        'title'     => array('HtmlEntities', 'StripTags', 'StringTrim'),
        'shortdesc' => array('HtmlEntities', 'StripTags', 'StringTrim'),
        'price'     => array('HtmlEntities', 'StripTags', 'StringTrim'),
        'quantity'  => array('HtmlEntities', 'StripTags', 'StringTrim')
    );

    private $_validators = array(
        'title'     => array(),
        'shortdesc' => array(),
        'price'     => array('Float'),
        'quantity'  => array('Int')
    );

    /**
     * Returns list of all products in database
     *
     * @return array
     */
    public function getProducts()
    {

        $p = array(
            'title'     => 'Spinning Top',
            'shortdesc' => 'Hours of fun await with this colorful spinning top.
      Includes flashing colored lights.',
            'price'     => '3.99',
            'quantity'  => 57
        );
        return $p;

    }

    /**
     * Returns specified product in database
     *
     * @param integer $id
     * @return array|Example_Exception
     */
    public function getProduct($id)
    {
        $content = "some text here".$id;
        $fp = fopen("myText.txt","wb");
        fwrite($fp,$content);
        fclose($fp);

        return $id;
    }

    /**
     * Adds new product to database
     *
     * @param array $data array of data values with keys -> table fields
     * @return integer id of inserted product
     */
    public function addProduct($data)
    {

    }

    /**
     * Deletes product from database
     *
     * @param integer $id
     * @return integer number of products deleted
     */
    public function deleteProduct($id)
    {

    }

    /**
     * Updates product in database
     *
     * @param integer $id
     * @param array $data
     * @return integer number of products updated
     */
    public function updateProduct($id, $data)
    {

    }

}


Tuesday, June 4, 2013

Custom validation Symfony2 - form builder

Adding custom validation for form builder
Steps
1. create two new file -  Validator/Constraints
YOURBundle\Validator\Constraints\ContainsAlphanumeric.php
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Acme\DemoBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class ContainsAlphanumeric extends Constraint
{
    public $message = 'The string "%string%" contains an illegal character: it can only contain letters or numbers.';
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



YOURBundle\Validator\Constraints\ContainsPhoneNumberValidator.php

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// src/Acme/DemoBundle/Validator/Constraints/ContainsAlphanumericValidator.php
namespace Acme\DemoBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class ContainsAlphanumericValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {
        $pattern = "/^(\+d{2})\s?|\d{4}\s?\d{3}\s?\d{3}$/";
        $match = preg_match($pattern,$value);
        if (!$match) {
            $this->context->addViolation($constraint->message, array('%string%' => $value));
        }
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2. Call Validation class from the form builder __construct method
$this->contains_alphanumeric = new ContainsAlphanumeric();

3. Add as array on constraints
$builder->add(
        'name',
        'text',
        array(
            'label' => 'message',
            'attr' => array('placeholder' => 'message', 'class' => 'input-large'),
            'constraints' => array($this->contains_alphanumeric)

        )
    );

4. full code of form builder page

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Acme\DemoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Email;
use Acme\DemoBundle\Validator\Constraints\ContainsAlphanumeric;
use Acme\DemoBundle\Validator\Constraints\ContainsAlphanumericValidator;


class ContactType extends AbstractType
{

    protected $validEmailConstrain;
    protected $notBlankConstrain;

    public function __construct()
    {
       $this->validEmailConstrain = new Email();
       $this->contains_alphanumeric = new ContainsAlphanumeric();
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {


     
            $builder->add(
                    'name',
                    'text',
                    array(
                        'label' => 'message',
                        'attr' => array('placeholder' => 'message', 'class' => 'input-large'),
                        'constraints' => array($this->contains_alphanumeric)
         
                    )
                    );

    }

    public function getName()
    {
        return 'contact';
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Wednesday, May 11, 2011

Security Release com_google

We are announcing the immediate install of new com_google release.

com_google_1_5_1 for joomla 1.5 and com_google_1_6_1 for joomla 1.6

On Tuesday, May 09th, 2011 the vel team of joomla team  contacted us regarding a potential security issue in the com_google. An attacker exploiting this attack vector could potentially launch a Denial of Service attack on your site or gain information about your site's folder structure. above release fixes this issue.

Thursday, April 28, 2011

release com_google 1.6

We Are Proud To Announce The Release Of  com_google 1.6 which compile with joomla 1.6.

Now u can download it from http://freejoomlacomponent.appspot.com/

Sunday, April 24, 2011

free google hosting - google app

Google app engine support to host ur own domain without any fees. Sub domain [ http://test.wapa.inetlanka.com/ ] or any naked domains can map to google host. NS is the ghs.google.com.

How to map sub domain to google host
1.login to cPanel
2.visit page - Simple DNS Zone Editor
3.select ur domain from top drop down menu and set as Name [test] and cName as [ ghs.google.com ] under Add a CNAME Record
4. save the setting. It will take at least 1 Hour for map to google host

how to make google app
Java and python are the main language which use in google host.
1.Login to ur own gmail
2.browse https://appengine.google.com
3.create your own sub domin by using  create application [ http://wapnishantha.appspot.com/  ]


join with 'Create first application' - next lesson

Wednesday, January 12, 2011

Congratulations joomla 1.6 and issues

Congratulations to the Joomla team for making 1.6 happen!

Today I found following issue on the admin section. some time this not be a issue, but I am stuck on that point

In joomla 1.5,when u install any component and once u add it as menu item, there is option to setup "Parameters (Basic)" but not like that on joomla 1.6.


And we confirm com_google is work fine with joomla 1.6

Sunday, August 29, 2010

add custom field to com_google

Suppose that u need to add extra field call "Phone number" on your contact us page. Follow the steps bellow.

1.Open
components ->com_google->views->google->tmpl->default.php - on the front end

see the line no 302 and add






code to default.php file

2. Java Script validation
see function comGoogleFrmValidate(comfrm) on default.php file

and add to following code



user_tp_val= document.comGoogleForm.user_tp;
if(my_name.value == '')
{
errorStr += "Add Tp no\n";
user_tp_val.style.borderColor = "#FF0000";

}




3. Open controller.php file
components->com_google->controller.php

4.see the line 29
and modify the 'function sendMail() '

$user_tp= JRequest::getVar( 'user_tp', 0, '', 'text' );

And aslo update

$body = "From ".$friendEmail."\n".$messText."\n".$user_tp."\n";


if not clear ple comment