Search This Blog

Monday, July 30, 2012

Getting Started With Zend Framework In Ubuntu

In one of our earlier posts we have told you how to install the PHP based Zend application Framework on Ubuntu. But in that post we just showed you how to install the framework. In this article we will tell you how you can get started with developing your projects in the Zend PHP framework using Ubuntu.
After you you have installed Zend on your Ubuntu machine using apt-get, you can find the installed Zend client library files at this location:
/usr/share/php/libzend-framework-php/
The total directory size is a meager 32 MB, so you should not worry about this. Now that we know where the library files are, lets get started with creating our first Zend project in Ubuntu.
Zend has a nice command line based tool called the zf or zf.sh using which you can create new projects and add views and controllers to your projects. The best part about these command line tools are that they reduce your code writing by creating all the required project files and populating these files with the bare minimum code snippets!
For eg, if you want your Zend project (which is for instance named - myproject) to reside in the /var/www/myproject folder then navigate to the /var/www folder and issue this command from the command prompt:
zf create project myproject
or
zf.sh create project myproject
You can now see that five new directories - application, docs, library, public, tests have been created inside the /var/www/myproject folder. These folders contain some files - controllers, layouts etc about which we will tell you in another post.
The entry point for your new Zend project is from the index.php file which is located in the /var/www/myproject/public folder.
You will be now able to access your new project at this url (provided you are using your default hosts and apache configurations):
http://localhost/myproject/public/
However at this moment, if you access the project URL mentioned above, you may see an error. There is one last thing we need to do in order to fix it and get started. Open the index.php file located inside the /var/www/myproject/public folder and paste the following code snippet just above the line that says - /** Zend_Application */
set_include_path(get_include_path().PATH_SEPARATOR.'/usr/share/php/libzend-framework-php');
The above code snippet tells our app the exact location of the Zend libraries which we just installed. Now, if you reload the page, you can see the Zend Welcome page. We will tell you how to remove this welcome page in our next Zend for Ubuntu tutorial.

Installing PHP and Zend Framework on Ubuntu

Installing PHP and Zend Framework on Ubuntu

Print PDF
I had a lot of trouble setting up Zend Framework. The installation procedure is very simple, but making the custom controllers and routes work isn't that easy.
Installing a PHP and Apache server on linux is very easy: just find and install the following packages (or open the terminal and run sudo apt-get package_name for each package):
apache2
php5
The directory for websites should be located at /var/www/ and the configuration files for apache in /etc/apache2/ and for php5 in /etc/php5/. To install Zend Framework, install the following package:
zend-framework
Now let's create a Zend project. Open the terminal and run:
zf create project path_to_project project_name
As described on official Zend Framework site, you should create a virtual host for your zend project. This way, you'll access the site at subdomain.localhost. Open /etc/apache2/sites-enabled/000-default (which is a symlink to /etc/apache2/sites-available/default) and add the following lines:
NameVirtualHost *:80

	ServerName subdomain.localhost
	DocumentRoot /path/to/zend_project/public
			
	/path/to/zend_project/public>
                Options Indexes FollowSymLinks MultiViews
		DirectoryIndex index.php
		AllowOverride All
		Order allow,deny
		Allow from all
	
The AllowOverride All option allows URL rewriting, which is required by Zend Framework. You should enable the mod_rewrite apache2 module (by default is disabled). This can be done by:
cd /etc/apache2/mods-enabled
sudo touch rewrite.load
sudo gedit rewrite.load
and add the following line to the opened file:
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
Finally, restart the apache2 server to enable the new settings:
service apache2 restart