Monday, June 17, 2013

Setting up Zend Server 6.0.1, Zend Studio 10.0.1 and the ZF2 Skeleton Application

I been writing on the Zend Framework and PHP for a more than 3 years now. Setting up a development server has always been a bit of a challenge. As any good organization knows you should document the setup so that new environments can easily be brought up especially as new hires join the team. There a bits of information that I have had to get off the web each time I set up a new development environment. I am documenting it here and hope it can help others as others have helped me.

My environment is Windows 7 64-bit. The first step is to install the Zend server. I installed the latest version 6.0.1. I noticed that there was no longer a Community Edition offering from the Zend website. I installed it anyway from here and when the trial version ran out it reverted to the free edition. Installing Zend Server will also install Apache2.2.22.
I then installed Zend Studio 10. You can do the same and use the trial version of Zend Studio or just down load and use eclipse PDT.

All my Zend tools are installed in C:\Environ\...










I also like to know where the Zend framework is installed for a number of reasons. Mostly because by default the Zend Server environment and the skeleton app work in concert to provide you with an environment link to the server instance of the Zend Framework 2 library. I'll show you where this is done later. The image below shows the location of the Zend library. For my installation it it located in: C:\Environ\Zend\ZendServer\gui\vendor\ZF2



Next...
Download the zend skeleton application version 2.1.0 located here: https://github.com/zendframework/ZendSkeletonApplication/tree/zf/release-2.1.0

Don't download the master skeleton application because it is using ZF2 2.2.0rc1 and Zend Server is using 2.1. You will get a bug. For more information see the following:
https://github.com/zendframework/ZendSkeletonApplication/commit/80884b7da866f5136654c83604c1270a3228b7b9

Unzip the skeleton application into the htdocs folder of your server and name the project folder zf2-tutorial:











Now add the project to your eclipse work space or Zend Studio workspace by choosing File -> New -> "PHP Project From Existing Directory". At this point you should be able to browser the project files in Zend Studio or Eclipse.

At this point you should have Zend Server, Zend Studio or Eclipse PDT, and the skeleton application ready to go.

Understanding the Environment Variable for The ZF2 Library
Once you see the zf2-tutorial project in the project explorer o fZend Studio or Eclipse PDT open it up and you will find in the public folder the index.php file take note of the following:

public/index.php
...
// Setup autoloading
require 'init_autoloader.php';
...

The init_autoloader.php files sets the zf2_path for you from an environment variable and then adds it to the autoloader. Open the init_autoloader.php file located at the project root:

init_autoloader.php
...
$zf2Path = false;

if (getenv('ZF2_PATH')) {           // Support for ZF2_PATH environment variable or git submodule
    $zf2Path = getenv('ZF2_PATH');
} elseif (get_cfg_var('zf2_path')) { // Support for zf2_path directive value
    $zf2Path = get_cfg_var('zf2_path');
} elseif (is_dir('vendor/ZF2/library')) {
    $zf2Path = 'vendor/ZF2/library';
}

if ($zf2Path) {
    if (isset($loader)) {
        $loader->add('Zend', $zf2Path);
    } else {
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
        Zend\Loader\AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true
            )
        ));
    }
}
...

In the default installation of Zend Server the environment variable is set in the php.ini file located here:

You can search this file for zf2_path and should find this:
zf2_path="C:\Environ\Zend\ZendServer\share\ZendFramework2\library"

IN the init_autoloader.php file the PHP call to get_cfg_var will return this path. If you want you can also set the zf2_path in the windows environment variables as well by using the set command on the command console. The last test checks the local project "vendor" folder to see if you included it directly. It is important to note that if you deploy to a server that does not run Zend server you will need to add the ZF2 Library to the vendor folder:










This should give you a good idea how Zend server includes the library as well as how you can deploy to a host that doesn't run the Zend server. If you are a Zend Master you may also debug into the library and/or browse the library. In that case I suggest copying the library to the vendor folder.


Vhost  and Windows Host Setup

VHOST
Next we need to make sure the application runs in our browser and in our local debugger so we need to setup a vhost. This is one area that seems to get a lot of attention as it can be a little challenging. You need to be aware of both the virtual host setup, the htaccess files for your project (found in the public folder), and windows host mappings as well. The zend tutorial series does a really good job with this already so I suggest you take a look at those instructions found here as well.

At this point you may or may not need to setup the httpd.conf file found in the 'Apache2/conf/' folder:



I did not have to make changes. However you may need to change AllowOverride None to AllowOverride FileInfo. 

Of significant important  is the end of the httpd.conf file where you will find the following line:

Include "C:\Environ\Zend\ZendServer/etc/sites.d/vhost_*.conf"

This line tells the server to load vhost files with the pattern "vhost_*.conf".
So by adding the contents define below to a "vhost_zf2-tutorial.conf" we should be all set for our project.

<VirtualHost *:80>
    ServerName zf2-tutorial.localhost
    DocumentRoot C:/Environ/Zend/Apache2/htdocs/zf2-tutorial/public
    SetEnv APPLICATION_ENV "development"
    <Directory C:/Environ/Zend/Apache2/htdocs/zf2-tutorial/public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Location of the vhost_zf2-tutorial.conf file:



WINDOWS HOST:
The last thing you need to do is  update c:\windows\system32\drivers\etc\hosts file so that zf2-tutorial.localhost is mapped to 127.0.0.1. 









hosts
...
# localhost name resolution is handled within DNS itself.
127.0.0.1       localhost

127.0.0.1       zf2-tutorial.localhost
...

Note: The host file was locked for editing. I had to copy the file to the desktop and edit it with notepad before deleting the old and copying the new one from the desktop.

If you restart your webserver you should be able to browse to http://zf2-tutorial.localhost/










Tuesday, February 5, 2013

Zend Framework 1 or 2? Check-box and Label Alignment with CSS

Greetings programs,

I have been programming on the Zend Framework for a few years now. I have put off solving some very simple but super annoying problems. Well, I have found some time and am finally going to solve a problem that has been a major problem for my Zend Forms. "Checkbox and label alignment".

Like most Login forms I have a 'remember me' check box. Every time I use/reuse this form the check box is below the label getting tangled up in my submit button. This is just annoying. It turns out the best way to solve the problem is with CSS and it's really simple.

So here is my form Application_From_User_Login function for creating the check box. Nothing out of the ordinary here.
...
I have an abstract decoration that decorates any zend form element that I put on a form in a consistent manor. The call to _decorateCheckBox($remember) creates the html format for this particular check box and all other check boxes i might use. So lets' look at that code below.
...
This decorator creates a structure that looks like the following:...
...
This is what it looks like when I render the form in my application. Notice the check box is below the label.

The solution is simple. The label has to be styled 'inline'. Here is the CSS. ...
The resulting login form looks much better. Really simple fix.


Tron: My User has information that could... that could make this a free system again! No, really! You'd have programs lined up just to use this place, and no MCP looking over your shoulder.