Monday, July 15, 2013

Create new controller on ZF

In this tutorial I'll explain how to create new controller and do more interesting stuffs. As I explained in previous post, for most important of controller, that requires directory with same name of controller and inside that, view pages with same name of all actions of controllers. But, this is automatically managed by ZF command line tool. And in Netbeans IDE there is more good interface for executing commands

- in Netbeans project panel --Right Click on project > Zend > Run Command... and interface will be open.


  • First parameter is "Filter": That similar to search particular command from all command list.
  • Matching Tasks: This will show you commands matching your query entered in Filter textbox. Select proper command from list. There is text area below list of commands, it'll show you description.
  • Enter required parameters in "Parameters" textbox.
  • At very bottom, there is "commad", it'll show you final output command. 
  • and press Run to execute command.
  • For example, I've created controller named "info".










[controller]

[view]


As Shown above both images, you can see new created controller: "InfoController.php" and for that controller there is "info" directory with .phtml files of actions of controller.

Now, let's modify Info Controller:
- Remove content of index.phhtml and edit indexAction in Controller.








In any controller there is init() and indexAction() as by default. You can create as many functions you want. When any controller executes init() method always executes first, no matter which action you are executing from that controller.

Now, Lets create another action in info controller with its view:


and add some message in anotherAction() function...

and execute...


That's it. This is enough to get started with ZF Controllers.

Controller understanding in ZF

When we create new ZF project, it creates two controllers [ IndexController.php & ErrorController.php ] by default. Controller as also called as Action Controller, because it controls all actions. Controllers are located in the application/controllers  directory. The action controller is concerned with out application's control logic and is part of the 3-tier separation the Model-View-Controller offers.

Default Example of controller:

class IndexController extends Zend_Controller_Action                                                                    
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }


}



Most Important:


Once one controller is created, then there is some thing you must care about. For example, for IndexController.php, with controller its view file is also created. As shown in image, there is naming conversation for that also. Views directory stores all view files of application. Inside views/scripts,  there must be directory with the same name of controller.
So, in this example there must be index directory. And based on action method in controller there should be view file. So, one view file for one action. So, here in this example, there is indexAction in IndexContoller.php, so, in views/scripts/index there is index.phtml


How controller executes:

We can directly call controller from addressbar. example: 
http://zf2-local.com executes indexAction() of indexController.php.
so, if you execute http://zf2-local.com/index/ then also output will be same. as like that, 
http://zf2-local.com/index/index will also give same output.






In next tutorial we'll modify controllers.

Understanding Directory Structure in ZF

When we create new project in Zend, it creates directory structure for us. On right side image, full directory structure of ZF application is shown.

This structure has four main area, application, library, public, and tests.

Application Directory: Is responsible for holding our application-specific files such as configs, controllers, models, and views. These are basic in any zend framework application,  in some zf application there is modules directory, holds different modules required for application. So, This application directory contains main MVC files.

Library directory: Is responsible for holding our supporting classes that not come into the scope of model. Inside library, we have the zend directory that contains the ZF source files.

Test directory: Stores our tests for our application.

Public directory: Is responsible for holding all of our publicly accessible assets such as images,CSS, and JavaScript.


Bootstraping [Bootstrap.php]:  This refers to the process of application initialization where we configure, and startup, the MVC process when someone requests a page.

The Index file [index.php]: This file is created under public directory, which is the main entry point for all of the requests to our application. This file points to application, library directory and application environment of application.

.htaccess file: This is important file in application, that rewrites rules for routing.

Application Configuration [application.ini]: This is most important for zf application. This is heart of application. Because, it contains all configurations about our application like php_errors, database connection, & path application, controller, library directory and all routing mechanisms are written here.



Zend Tutorials

Starting New Zend Framework Project

Hello, I'm back with new Zend Framework Tutorials. Go through these given links before directly starting from this tutorial:


In, this tutorial I'll explain how to create new Zend Project on your server with NetBeans IDE. Follow the steps as show in image below:
    [Image 1]

    [Image 2]



















[Image 3]

    [Image 4]


    


























[Image 5]


If you see directory structure like image 5, then congratulations...!!! your  Zend Project is ready to start.

Other ways to create project:
Zend Framework having powerful command line interface for doing work more easily. In Zend framework library you downloaded, there is "bin" folder contains zf.sh and zf.bat files.
- First go to the directory where you want to create your project.
- path-to-bin/zf.bat  create  project  project-name

Now, I created virtual-host named http://zf2-local.com.  This virtual host points to my public folder not root folder of project.




That's it, if you are able to see this type of page, then your created project works very well.



Tuesday, July 9, 2013

Create Virtual Host


Webmin is the best server admin panel, but some times it fails to do some jobs. Creating virtual host is one of them. So, I’m explaining code for creating virtual host. This is very easy to do. Just copy paste given code and that’s it.

Step 1) Open Apache2 configuration file

sudo gedit /etc/apache2/apache2.conf

Copy-paste this code:

######################wordpress-local.com -- start ###############
<VirtualHost *:80 >
ServerAdmin webmaster@localhost
ServerName wordpress-local.com
DocumentRoot /var/www/wordpress

<Directory >
  Options All
  AllowOverride All 
</Directory >

<Directory /var/www/wordpress>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Order allow,deny
  allow from all
</Directory >


ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
< Directory "/usr/lib/cgi-bin" >
  AllowOverride None
  Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  Order allow,deny
  Allow from all
</Directory >

ErrorLog ${APACHE_LOG_DIR}/error.log

LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
< Directory "/usr/share/doc" >
  Options Indexes MultiViews FollowSymLinks
  AllowOverride All
  Order deny,allow
  Deny from all

Allow from 127.0.0.0/255.0.0.0 ::1/128

   < /Directory > 

< /VirtualHost >

######################wordpress-local.com -- over ###############

Step 2) Open  Hosts File

sudo gedit /etc/hosts

Write this code

127.0.0.1 localhost
127.0.0.1 wordpress-local.com     <<----- 

Step 3) Restart Apache server

sudo service apache2 reload

Now, you can open http://wordpress-local.com on your browser.