Blog Zend Zend 2

Ajax, ZF2 action and json return

On the first project with Zend 2 I needed to incorporate Ajax. I made my view action, view script and in it added ajax that had set url to another action in the same controller. On the click of the button the data had to be sent via ajax and then new set of data had to be returned in form of json. Here came the problem. No matter what I did, the returned data wasn’t in json format. It contained, exept the json, the entire layout script from Zend 2. Naturally I resorted to doc Google to search for remedy to my problems.
I didn’t find much, expect the way to incorporate Json view. But after some time I gave up on the idea – didn’t seem really practical. I just wanted something simple and quick. During my search I stumbled upon the advice to get response and set its content. So I tried it and it worked!

Here is the simplest way to set json as your response:

    // some controller

   public function myajaxAction(){
    //....parsing of the received data


    return $this->getResponse()->setContent(json_encode($return));

Voila! 🙂

Blog Zend Zend 1

Admin area and module activation – Part 2


In the last article I showed you how we solved the problem of activating modules in admin area of our Zend web application. In Part 1 I presented the logic behind the activation, now let’s list our modules in admin area and change their states.

For modules listing I’ve made a new model called Application_Models_Module and I’ve situated it inside application/models/ folder in Module.php file. This model will have functions for activating my module and listing all modules. I wanted to make the process as automatic as possible, so here is how my function look like:

//this goes into application/models/Module.php file
class Application_Model_Module {

     protected $_mod;
     protected $_name;
     protected $_active;

     // basic constructor, setters and getters go here
     // ...

     public function getModules($path){                                // function to get all modules, it takes modules path as it's variable
          $directories = glob($path. '/*' , GLOB_ONLYDIR);                  //get directory listing 
          $directories = str_replace($path.'/','', $directories);           //get only directory names

          $config = new Zend_Config_Ini(APPLICATION_PATH.'/modules/config/modules.ini');  //get my modules.ini file with basic module info

          $mods = array();
          foreach($directories as $mod):
               $conf = $config->$mod;                  //get configuration from modules.ini file for single module

               $this->setMod($mod);                    //setting data inside model's variables

               $mods[$mod] = $this;                             //add modules setting to mods array


          return $mods;                                         //return all modules' data

Ok, we have function that gets our modules. Now we’ll create the function to activate our modules. This function has to read the contents of modules.ini file and then edit them accordingly:

//appending application/models/Module.php file
class Application_Model_Module {

     //right below the getModules() function
     public function changeState($mod, $state){                                    //takes as parameters module and the future state (0 or 1)
         $config = new Zend_Config_Ini(APPLICATION_PATH.'/modules/config/modules.ini', null,          //read the contents of modules.ini and allow modifications
                    array('skipExtends'  => true,
                    'allowModifications' => true));
         $config->$mod->module->active = $state;                       //set module's state
         $writer = new Zend_Config_Writer_Ini(array('config'   => $config,               
				'filename' => APPLICATION_PATH.'/modules/config/modules.ini'));

         try{                  //try to write the state in the file if not successful return error
             return $state;
         }catch (Exception $e){
             return $e;

All of needed function are now complete and we’re going to edit our view. In my admin area module controllers folder I’m going to make a controller Settings and inside it I’m going to add action modulesAction(). In it we’re going to list all of our modules.

// Settings controller inside admin module

class Admin_SettingsController extends Zend_Controller_Action {

     public function modulesAction(){
          $mods = new Application_Models_Module();

          $mods = $mods->getModules(LOCAL_PATH); //LOCAL_PATH is path to my modules/local folder, you can define it as a global in index.php

          $this->view->mods = $mods;     // put the list of mods in the view variable

We’re going to use our view variable to show the modules and edit their states with a little bit of ajax magic inside the view file. Here’s my view file:

 <!-- contents of application/modules/admin/views/scripts/settings/modules.phtml file -->
$mods = $this->mods;
<div class="title">
	<h1>Installed Modules</h1>

<table class="mods">                        //simple table to show our modules list with enabled/disabled information

<?php foreach($mods as $mod):?>

	<a href="/admin/settings/module?mod=<?php echo $mod->getMod();?>" class="<?php echo $mod->getMod()?>"><?php  echo $mod->getName()?></a>

	    <?php $state = $mod->getActive()?>
	    <select class="sel-mod">                                               //select if the module is enable or disabled
		<option value="0" <?php if(!$state) echo 'selected = "selected"' ?>>Disabled</option>
		<option value="1" <?php if($state) echo 'selected = "selected"' ?>>Enabled</option>
	<small class="message"></small>                                            //message area for "Module disabled/enabled." message
	<img style="display: none;" src="<?php echo $this->baseUrl('path_to_small.gif');?>"/> //small gif for loading
<?php endforeach;?>

As you can see we have made a simple table that shows us some basic data. Now let’s make the script needed for changing the state using ajax.

//add this to the bottom of view file or make a new script file and add it at the bottom
	$('.sel-mod').on('change',function(){                                         //when select state changes
		var mess = $(this).parent().find('.message');
		var img = $(this).parent().find('img');

			url:'/admin/settings/modulestate?format=json',                //where to send the data
			beforeSend: function(e){
				img.attr('style','display:block');                    //before sending show the loading gif
			data: {                                                       //data we need to send
				mod: $(this).parent().prev().find('a').attr('class'),
				name: jQuery.trim($(this).parent().prev().text()),
				state: $(this).val()},
			success: function(e){                                          //what to do on success
				var res = e.result;
				var text;
				if(res==1) text = 'Module enabled.';                  
				else text = 'Module disabled.'

				mess.text(text);                                      //inject message into message div
				setTimeout(function(){img.attr('style','display:none')},500);  //disable gif when not needed
			error: function(e){                                           //what to do on error
				mess.text('Something went wrong, please try again later');
			dataType: 'json'                                             //type of data to use

We need to make another action in our settings controller, the one that saves data into the modules.ini file. It’s the action that ajax uses as url to which to send data. Since we’ve already completed our model all we need is to instantiate the right object and call changeState() function.

 //this goes inside our Settings controller

   public function modulestateAction(){
        $mods = new Application_Model_Module();            //our object
        $params = $this->getRequest()->getParams();        //get the request params
        $mod = $params['mod'];                             
        $state = $params['state'];
        $this->view->result=$mods->changeState($mod, $state);    //change the state and return result into new view variable

Don’t forget to make the view file of your new action. And you’re all set. All that needs to be done is to style your table to your liking and add gif file of your choice.

Cheers! 🙂

Blog Zend Zend 1

Admin area and module activation – Part 1

I recently needed to make module activation in the admin area of our online booking system that we’re developing. Here is how I solved it.

Our online booking system is based on Zend framework, and for now doesn’t deviate much from Zend’s project structure. Our modules are situated in application/modules folder. We’ve already made admin area as a module when we started to work on incorporating other modules with our system. And now the question was asked – how do we enable and disable modules from our admin area? Our admin area is already a module itself so we cannot make new modules as a part of our admin module. New module itself also has to show certain information (forms) on the frontend and backend, how to include it to do exactly what we need? I then stumbled on the solution to make admin area as admin controller but discarded it after some thought. We already have working admin area of our system so why should we change it? Do all the work again and then come to basically almost the same point. So I played a bit with various versions of module activation and files, and came up with this idea.

I’m going to make a config file that has some basic information about the module that is going to be filled during the installation process of the module itself. At that time I filled it by hand, and I oped for .ini type of file. But you can easly make it as a yaml or xml file. The following code is situated in modules.ini file in application/modules/config folder. My booking module is situated in application/modules/local folder.
Basic info in .ini file is consisted of module folder name, module name and is module active info. Here’s how my basic info looks like:

<code class="language-php">// this code goes into modules.ini file
[booking]                                           // marks new part of modules.ini file, same as module folder name = "Booking" = "1"

Now that was the easy part 🙂 . Let’s get on to the hard part.
We want our application to show enabled and not show disabled modules, so we need to write something in our bootstrap file. I presume you know how to detect modules in /modules folder using your application.ini file. Since our module is situated in /local folder inside the /modules folder we have to designate that folder also as our modules folder. First we’ll make public function _initMods() in our Bootstrap.php file. Since we’re going to need some things for module activation we’re going to put them at the beginning. We’re going to instance our front controller for later use and then ‘read’ the names from /local folder.

<code class="language-php">// this code goes into application/Bootstrap.php file
public function _initMods(){
        $config = new Zend_Config_Ini(APPLICATION_PATH. '/modules/config/modules.ini');  // new object of Zend_Config_Ini, 
        //we use it to read our modules.ini file
        $directories = glob(APPLICATION_PATH .'/modules/local/*' , GLOB_ONLYDIR); //grab our module directories from /modules/local folder
        $directories = str_replace(APPLICATION_PATH .'/modules/local/','', $directories); // we'll need only names, not paths
        $this->bootstrap('FrontController');                      // we need FrontController to add new module directory
        $frontctrl = $this->getResource('FrontController');       //  get FrontController and all of its current properties

now we have everything we need to add new module. We bootstrap the FrontController in order to get everything that is currently set in it. This proved to be crutial for adding new modules folder. Here’s the code for adding new active module. I’ll explain it in comments and further bellow:

<code class="language-php">// this code goes into application/Bootstrap.php file
public function _initMods(){
// .....
foreach($directories as $name){
        	if($name != "admin" &&  $name != "local" && $name != "config" ){ // we don't need to activate these modules
        		$active = $config->$name->module->active;            //here we get the = ? value
        			if($active){                                 // if = 1 
        			$frontctrl->addModuleDirectory(APPLICATION_PATH .'/modules');
        			$frontctrl->addControllerDirectory(APPLICATION_PATH .'/modules/local/'. $name  .'/controllers',$name);     // add module directory into the list of controler directories
// in order for our module to be fully functional we have to bootstrap it's Bootstrap.php file. Only then our module
// is seen as a module by Zend Framework
        			require_once APPLICATION_PATH .'/modules/local/'. $name .'/Bootstrap.php';  // get the contents of our modules's Bootstrap.php
        			$className = ucfirst($name) .'_Bootstrap'; 
        			$moduleBootstrap = new $className($this);   // make new instance of our module's Bootstrap.php
        			$moduleBootstrap->bootstrap();              // bootstrap our module's Bootstrap.php


And now we have initialized our active modules. If we try to access the modules with we’ll get an error. If a module has it is initialized and we can access our modules functions and controllers.
For the next part I’ll show you how to make activation from admin area.

Cheers! 🙂