Thursday, 31 December 2015

Zend Framework 2 Json Model understanding

Zend Framework 2 Json Model understanding


Question: How to render JSON response?
In controller use, JSONModel
public function indexAction()    {
        $result = new JsonModel(array('data'=>array(
     'key1' => 'value1',
     'key2' => 'value2',
     'key3' => 'value3',
     'key4' => 'value4',
     'key5' => 'value5'
            
        )));

        return $result;
    }

In View file, use following the encode the array.
echo Zend\Json\Json::encode($this->jsonArray, Zend\Json\Json::TYPE_ARRAY);



Question: How to JSON Encode an php object in recursive way?
$arrayData = Zend\Json\Json::encode($phpObject, true);



Question: How to convert XML to JSON?
$jsonContents = Zend\Json\Json::fromXml($xmlStringContents, true);



Question: How to get PHP Array from JSON Encoded string in view file?
$encodedValue='[1,2,3,4,5,6]';
$arrayData = Zend\Json\Json::decode($encodedValue,Zend\Json\Json::TYPE_ARRAY);
print_r($arrayData );die;


Question: How to get PHP Object from JSON Encoded string in view file?
$encodedValue='[1,2,3,4,5,6]';
$arrayData = Zend\Json\Json::decode($encodedValue,Zend\Json\Json::TYPE_OBJECT);
print_r($arrayData );die;



Question: How to display JSON Encoded string in Pretty way?
$encodedValue='[1,2,3,4,5,6]';
$arrayData = Zend\Json\Json::prettyPrint($encodedValue, array("indent" => " "));
print_r($arrayData );die;



Question: How to check Ajax Request OR Not?
if($this->getRequest()->isXmlHttpRequest())){
/** 
This is Ajax Request 
**/
}else{
/** 
This is NOT Ajax Request 
**/
}



Wednesday, 30 December 2015

Zend Framework2 manage layout and view variables from controller

Zend Framework manage layout and view variables from view


Question: How to set variable to layout from controller?
Following are different three ways to set the variable from controller.
First way
public function indexAction(){ 
    $this->layout()->variableName = 'value1';
}

Second way
public function indexAction(){ 
    $this->layout()->setVariable('variableName', 'value2');
}

Third way
    public function indexAction(){ 
    $this->layout()->setVariables(array(
        'variableName1' => 'value1',
        'variableName2'  => 'value2',
    );
}

In layout File, you can access with following way.
$this->variableName1;


Question: How to set variable to view from controller in Zend Framework2
public function indexAction(){ 
return new ViewModel(array(
           'variableName3' => 'value 3',
         ));
}

in layout view, you can access with following way.
$this->$this->variableName2;



Question: How I can get access to my module config from the controller?
public function indexAction(){ 
    $config = $this->getServiceLocator()->get('Config');
}



Question: How to Change layout in the controller in Zend Framework 2.0?
public function indexAction(){ 
    $this->layout('layout/mynewlayout'); /*It will search view/layout/mynewlayout.phtml*/
}



Question: How to disable layout in zend framework 2?
public function indexAction(){ 
    $viewModel = new ViewModel();
    $viewModel->setTerminal(true);
}



Question: How to disable render view in zend framework 2?
public function indexAction()
{ 
    return false;
}