So in my last atricle we installed the Zend Framework on Ubuntu Server, now we will use this knowledge to make our first ZF2 app called ‘Hello World’. Not inspiring I know but it will help us to hit the ground running (so to speak).

Now down to work, first fire up the terminal window and change the directory to the project one we created in  ‘Installing Zend Framework‘. Now we should have a directory structure like:

The ‘vendor’ directory will house all our php libraries like Zend Framework so we don’t need to worry about that for now. We will need to add a few directories and files so the directory structure should look like (don’t worry about creating them now we will do this as we go):

So to start change directory to the tutorial on by

First we make the public directory

Now we make two files, one at a time in this directory, first one ‘.htaccess’ so

then add this

then press Ctrl O and then Ctrl X to exit the editor. The code in this file tells apache web server to redirect all requests to index.php as long as the requested filename does not exist else it will serve it. Now we create the index.php file and add our code to it, so


and the add this code

press Ctrl O and then Ctrl X to exit the editor.

OK so what’s happening here is lines 5-19 just initiates the .htaccess file if we are using the build-in web server that ships with php 5.4 (which we will use to test our app).

Line 25: here we change the directory to be the one above the public folder so everything all files are relative to our app root

Line 27: redefine a global constant ‘APPLICATION_PATH’ which will the app root as an absolute path.

Line 30-32: sets up are autoloading so we don’t have to add require_once('some_php_file.php'); to our php class files.

Line 34-36: checks whether we can load the Zend Framework, if not then we throw an exception

Line 38: we include then application config file and assign it to a variable called $config

Line 41: we now tell the MVC in Zend Framework to run the app using the config file.

So with that we now need to code the config file. so in the terminal again we make the needed directories

then we make a config file like so

and add this to it:

press Ctrl O and then Ctrl X to exit the editor

here we create an array tell which modules to load, as we will have only one for now we use ‘Application’ (we will create this module shortly) then we setup the module paths which will live in either the module folder or the vendor folder and that’s all we need for now and to load any config overrides in the autoload folder. You may of noticed that I am using a funny array syntax which as of php 5.4 we can use [] instead of array() for array creation.

Now we create a global override config file in the autoload folder so

and in this file we just return an empty array for now.

press Ctrl O and then Ctrl X to exit the editor. Now we are ready to create our first zf2 module we will tackle this in the next part in this tutorial.

  • I think this is one of the most significant information for me.
    And i’m glad reading your article. But want to
    remark on some general things, The website style is
    great, the articles is really great : D. Good job, cheers