Zend, MongoDB, Doctrine a perfect relationship 2 : Making a sample application

Installing and setup Zend framework with Mongo and Docrine in your local Linux machine

Frist of all you have to install all the requirements (prerequisites) for zend. Make sure that you have installed,

  • Latest PHP
  • Apache Http Server
  • MongoDB Server (http://docs.mongodb.org/manual/installation/)
  • php-mongo (http://www.php.net/manual/en/mongo.installation.php)
  • Git
  • These steps are related to configure zend skeleton app under Linux platform

  • change your directory path to a convenient location
  • cd /var/www/zend-test
    
  • clone the latest Zend skeleton application from github
  • $ git clone git@github.com:zendframework/ZendSkeletonApplication.git
    
  • move inside to your applicaion
  • $ cd ZendSkeletonApplication/
    
  • Inside your skelton application you can see following files are available
  • composer.json  composer.phar  config  data  init_autoloader.php  LICENSE.txt  module  public  README.md  vendor
    
  • Update and install zend framework inside your skeleton application
  • $ php composer.phar self-update
    Updating to version 3da....3ff.
        Downloading: 100% 
    	
    $ php composer.phar install
    Loading composer repositories with package information
    Installing dependencies (including require-dev)
      - Installing zendframework/zendframework (2.2.4)
        Downloading: 100% 
    	
    ....
    ....
    Writing lock file
    Generating autoload files
    
  • Mention your project dependencies in your composer.json file
  • $ vim composer.json
    "minimum-stability": "alpha",
    "require": {
    	"doctrine/doctrine-mongo-odm-module": "dev-master",
    	....
    	....
    }
    
  • Update your project with dependencies
  • $ php composer.phar update
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
      - Installing doctrine/lexer (v1.0)
        Downloading: 100%         
    
      - Installing doctrine/annotations (v1.1.2)
        Downloading: 100%         
      
    	........
    	........  
    
      - Installing doctrine/mongodb (1.0.3)
        Downloading: 100%         
    
      - Installing doctrine/mongodb-odm (1.0.0-BETA9)
                Downloading: 100% 
    
      - Installing doctrine/doctrine-module (0.7.2)
        Downloading: 100%         
    
      - Installing doctrine/doctrine-mongo-odm-module (dev-master 6d18d1f)
        Cloning 6d18d1fa915787d7adc39344d6bd987a29ca54e3
    
  • Create a virtual host for your project
  • create a file inside your /etc/sites-available/ folder named with your project(or any suitable name)

    $sudo vim /etc/sites-available/zend-mongo-test1
    <VirtualHost *:80>
        ServerName zend-mongo-test.localhost
        DocumentRoot /var/www/zend-test/ZendSkeletonApplication/public
        SetEnv APPLICATION_ENV "development"
        <Directory /var/www/zend-test/ZendSkeletonApplication/public>
            DirectoryIndex index.php
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    
  • create a symbolic link under /etc/sites-enabled/
  • $ sudo ln -s /etc/sites-available/zend-mongo-test1 /etc/sites-enabled/zend-mongo-test1
    $ ls -al /etc/sites-enabled
    total 8
    drwxr-xr-x 2 root root 4096 Oct  7 16:40 .
    drwxr-xr-x 7 root root 4096 Aug 15 10:48 ..
    lrwxrwxrwx 1 root root   26 Mar  1  2013 000-default -> ../sites-available/default
    lrwxrwxrwx 1 root root   45 Oct  7 16:40 zend-mongo-test1 -> /etc/apache2/sites-available/zend-mongo-test1
    
  • Restart your apache server and you can see your sample skeleton application in your browser
  • $server apache restart
    

    Browse http://zend-mongo-test.localhost

    Zend, MongoDB, Doctrine a perfect relationship !

    In a previous post I have discussed about CodeIgniter framework and how to build a simple app using MySQL with no ORM support. Zend is also a good PHP MVC framework which is most popular nowadays, after the Zend company introduced it newer version Zend 2. It has more robust and developer friendly enhanced features included such as routing, form creation, additional module support(3rd party) and many more.

    The NoSQL db has become a first selection nowadays, since the traditional server-side bulk database handling is getting frustrated sometimes ,but it is easy to deal with NoSQL because it has on-built robust and fast data execution feasibility, no need of very complex and disgusting queries to execute, and most suited for bulk data handling. Basically the best NoSQL giants are

    1. MongoDB
    2. Redis
    3. Apache CouchDB

    Those has its own and unique features , such as Redis not suitable for bulk data handling but suitable for real time communication web apps.
    But as a whole , all NoSQL systems primarily use JSON stuff for internal and external data processing. As an example for data insertions and retrieves they use simple JSON queries.

    Here we discuss about MongoDB as a NoSQL engine for our Zend application.

    So what is Doctine ? sometimes word makes you weird but it is an excellent PHP library for Object Relational Mapping (ORM) tasks. As we know Java use Hibernate and .NET use LINQ or NHibernate as their ORM tool, PHP not going to become second in ORM world, yes, it has Doctine!

    So, the time to discuss about a basic application using Zend,MongoDB and Doctrine, are you ready?

    First we discussed about configuration part of all those three units(The installation and configuration)

    Get Zend Framework .

    Basically, as a practice we use Zend Skeleton application to build a new application other than building Zend application from sketch.Its pretty easy , if you have git installed on your machine just clone it to your machine using this command

    git clone https://github.com/zendframework/ZendSkeletonApplication.git
    

    After clone it copy it to desired location(usually in Linux /var/www/ ,Aka-Document Root)
    (I think you already set up PHP+MySQL+Apache on your system, if not do it now, just in Ubuntu/Debian/Mint apt-get install php mysql-server apache2 ……)
    Then you need to install Zend inside this skeleton application, what you have to do is , go to your skelton application location using your terminal or cmd. You can see there are two files called composer.phar and composer.json is available in that directory.The composer.json includes the libraries/modules you need to download (simple dependency list) to the for application up and running. You may see Zend is already in your composer.json file.
    Just execute the command

    composer phar self-update
    

    or

    composer phar install
    

    If you like you can add a separate virtual host for your application, or other wise just develop it from your document root.For development purposes I recommend you to use NetBeans-PHP IDE to increase your development speed and for self understanding the structure and flow of the code.
    Other than that you may need to mod_rewrite on in your Apache configuration.

    If you are done, go to http://localhost/ZendSkeletonApplication to visit the pre-built Zend2 skeleton application.

    Get Mongo .

    Download it from http://www.mongodb.org/downloads and install or the easy way for Debian like systems is

    apt-get install mongodb
    

    Don’t forget to install mongo-php library because missing this thing is put you in a deep trouble,

    pecl install mongo
    

    Also you need to add line extension=mongo.so in your php.ini file (may be found it in two places just add that line in both, just search to find them).

    Check whether your Mongo library is bound with your PHP installation, (echo phpinfo();)

    So it is Doctine time .

    In order to install Doctrine module to your skeleton application you need to add Doctrine dependencies in your composer.json file , open it and add following line under require section,

    "doctrine/doctrine-mongo-odm-module": "dev-master"
    

    and change minimum-stability as,

    "minimum-stability": "dev",
    

    Then run

    composer phar update
    

    This will install Doctrine module under vendor section in your skeleton application (Zend library also goes to vender section)

    So if no errors up to now? you are almost done, then we discuss how to develop an application in next article.

    Thank you,

    Useful Links :

    http://framework.zend.com/downloads/skeleton-app
    https://wiki.10gen.com/pages/viewpage.action?pageId=5537953
    http://www.bigwisu.com/2012/10/03/zend-framework-2-doctrine-odm


    zf-logo-large


    images


    doctrineorm

     

    Install Mysql Apache PHP under Linux Environment

    Before continue with installation you may need to install gcc,make and all their dependencies.
    Notice: All three packages are going to install from source packages not from binaries.

    Install MySQL From Source.

    Dependencies-

    ncurces(http://ftp.gnu.org/pub/gnu/ncurses),
    bison(http://ftp.gnu.org/gnu/bison/),
    cmake(http://www.cmake.org/files/v2.8/cmake-2.8.9-Linux-i386.sh)

    Download MySQL source package Ex-mysql-5.5.27.tar.gz
    Unzip to /tmp directory

    shell commands

    cd /tmp/mysql-5.5.27
    groupadd mysql
    useradd -r -g mysql mysql
    cmake -G Unix\ Makefiles
    make &amp;&amp; make install
    cd /usr/local/mysql
    chown mysql .
    chgrp mysql .
    scripts/mysql_install_db --user=mysql
    chown -R root .
    chown -R mysql data
    cp support-files/my-medium.cnf /etc/my.cnf
    bin/mysqld_safe --user=mysql &amp;
    cp support-files/mysql.server /etc/init.d/mysql.server
    chown mysql /var/lib/mysql
    chgrp mysql /var/lib/mysql
    nano ld.so.conf
    

    add following lines if there is no such.
    /usr/local/lib
    /usr/local/mysql/lib

    ldconfig
    

    Install Apache

    Dependancies-

    Apr(http://apr.apache.org/download.cgi)
    Apr-util(http://apr.apache.org/download.cgi)
    Iconv(http://apr.apache.org/download.cgi)

    Download httpd-2.4.2.tar.bz2 from apache site

    Unzip into /tmp directory

    shell commands

    cd httpd-2.4.2
    ./configure
    make && make install
    nano ld.so.conf
    

    Add line
    /usr/local/apache2/lib

    ldconfig
    
    ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf
    ln -s /usr/local/apache/bin/apachectl /etc/rc.d/init.d/apache
    ln -s /etc/rc.d/init.d/apache /etc/rc.d/rc3.d/S90apache
    /etc/rc.d/init.d/apache start
    

    PHP installation

    Dependancies-

    OpenSSL
    GD
    libxml
    mcrypt/libmhash
    libpng/libjpeg/libxpm
    zlib
    mbstring
    curl

    Download php-5.4.5.tar.bz2 from php site

    cd httpd-2.4.2
    ./configure /configure --with-apxs2=/usr/local/apache2/bin/apxs --disable-debug --enable-ftp --enable-inline-optimization --enable-mbstring --enable-mm=shared --enable-safe-mode --enable-track-vars --enable-trans-sid --enable-wddx=shared --enable-xml --with-dom --with-gd --with-gettext --with-mysql=/usr/local/mysql --with-regex=system --with-xml --with-zlib-dir=/usr/lib --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mcrypt=/usr/local/lib --with-curl=/usr
    make && make install
    ln -s /usr/local/lib/php.ini /etc/php.ini
    

    Edit /etc/httpd.conf and set DocumentRoot wherever you want

    create a file called phpinfo.php in your document root which includes

    <?php
    php_info();
    ?>
    

    visit http://localhost/phpinfo.php and check whether all working fine !!

    Easy Web Site Development with Code Igniter (Part 2 Manage Views)

    By default in your views folder welcome_message.php is load when you type http://host/LBRVideos in your browser.To change this you need to add your own template to views folder.

    First create three php files in your views folder named header.php, home.php and footer.php.

    Add following sample code as an example.

    header.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Video Site</title>
    <script type="text/javascript" src="<?php echo base_url();?>scripts/jquery-1.7.2.js"></script>
    </head>
    <body style="background-image:url('<?php echo base_url();?>images/bg.gif');">
    <div>Links and Navigation goes here...<br/></div>
    

    home.php

    <?php $this->load->view('header');?>
    <div id="body-content">
    <div>This is the core page content...</div>
    </div>
    <?php $this->load->view('footer');?>
    

    footer.php

    <div><br/> © Copyrights Name goes here...</div>
    </body>
    </html>
    

    And we only need to load home.php because header and footer page loading functions are included in the home.php. The usage of using separate header and footer is if we have  pages with the same template no need of rebuilding the template again again, just use header and footer whenever it needed.

    Add new controller file called video.php (for example) in controllers directory.
    Add a function called index() and inside the function add line

     $this->load->view('home'); 

    Change Config -> routes.php -> $route[‘default_controller’] = “video”;

    Now you can browse your newly created site http://host/LBRVideos

    Make directory called scripts and add some css files to /scripts and link them to your site.
    Ex –

    <link rel="stylesheet" href="<?php echo base_url();?>scripts/customstyle.css"/>
    

    NOTE – base_url() will retrive the path http://host/LBRVideos

    Try with your own templates ….

    Easy Web Site Development with Code Igniter (Part 1 Installation)


    When we consider about web site development its really difficult to hard cording every thing using any of programming languages because it wast our time some times(Especially when you work on some quick delivery projects). So using frameworks especially MVC (Model-View-Controller) we can develop manageable, maintainable and quick projects with no stress manner.If you want to know more about MVC Patten read it here.

    Nowadays MVC Pattern used in every small-scale to large-scale projects not only in web development but also desktop and mobile development projects.

    • ASP.NET: MVC2,MVC3(Razor)
    • PHP: Code Igniter, Zend Framework,CakePHP
    • Java: Spring MVC

    Also C++,C#.NET use MVC Implementations in some projects

    CodeIgniter Setup Easy…….

    Download codeigniter from http://codeigniter.com/download.php
    You can get latest zip file named CodeIgniter_ver.zip
    Unzip it to your web root.
    Now Change the name of your CodeIgniter_ver folder to desired name. Ex- LBRVideos
    Open that using Eclipse or Netbeans (in Eclipse you may need to install PDT plugin)
    In NetBeans
    File -> New Project -> PHP -> PHP Application with Existing Sources
    Browse your CodeIgniter folder(LBRVideos) and you are done.


    In Eclipse
    Download and install PDT http://www.eclipse.org/pdt/downloads/
    (Help -> Install New Software)
    File -> New -> Other -> PHP Project ->Project Name : LBRVideos
    Right Click on Project Folder and select Import -> General -> File System -> Location to CodeIgniter Folder and you are done.

    Project structure ,

    Under application folder you can see models,views and controllers  folders

    models: Files under this folder usually used for modelling the data and pass to the controller(Ex- Database functions(CRUD Operations))

    controllers: Files under controller responsible for get the particular model data and pass to a particular view(load the view).

    views: File under that folder responsible for loading the html content plus information which is containing the model data passed by the controller and vise versa (Getting data -> modeling -> controlling -> viewing).

    Other than those three basic useful folders , you can see there are lot ..The config folder is the next most needed one. Inside config folder , open the config.php and change base_url to your host(may be localhost). Change Encryption key also.

    Ex-

    $config['base_url']    = 'http://myhost/';
    
    $config['encryption_key'] = '0779906999';
    

    Next open database.php file and configure it into your settings.

    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = 'root';
    $db['default']['database'] = 'lbrVideos'; //database nameof your site
    $db['default']['dbdriver'] = 'mysql';
    

    Next Post will be adding sample view(html code template to your site)….