Add additional controller actions


I assume you will be now able to create a simplest application using zend  framework (Hope first post of this blog  First application in zend in 3 easy steps helpful to create that one).

Now in your indexController you have a indexAction, which is default calling as we create virtual host in httpd.conf (of apache web server ). But if you wants to create more actions in indexController and worried what to do, then below is one solution, as we are not using any editor(like zend studio of eclipse ), we can create new actions in Index controller through following command on your command prompt -

zf create action add Index
zf create action edit Index
zf create action delete Index

These commands create three new methods: addAction, editAction and deleteAction in IndexController and also create the appropriate view script files(see your /path/to/myproject/application/views/scripts/index there should 3 new .phtml files) .

You now have four actions in your controller.

You can test your work by insert following URLs in your browser.

http://localhost:8181/mywork --> leads you to index controller's index action
http://localhost:8181/index/add --> leads you to index controller's add action
http://localhost:8181/index/edit --> leads you to index controller's edit action
http://localhost:8181/index/delete --> leads you to index controller's delete action

( you have to write your own code in indexController for approtiate result in your add/edit/delete page :-D  )

First application in 3 steps | first zend framework application

Follow the below steps and i assured you will got succeed for your first zend framework application.

Step 1 - Install zend framework :-
There are two ways to do this -
(a) Install zend server - Zend Server has native installers for Mac OSX, Windows, Fedora Core, and Ubuntu, as well as a universal installation package compatible with most Linux distributions.

After you have installed Zend Server, the Framework files may be found under /usr/local/zend/share/ZendFramework on Mac OSX and Linux, and C:\Program Files\Zend\ZendServer\share\ZendFramework on Windows. The include_path will already be configured .

to include Zend Framework.

(b) If  you do not wants to install zend server then you can download zend library form Zend library latest   and extract it where you wants in your PC.

Unpack the whole framework under a folder and add that folder/bin to you path.

So zend framework is installed on your computer now.

Test it on terminal (in Windows, Start -> Run, and then use cmd), type zf and press enter it should not show " 'zf' is not recognized as an interanl or external commnad, operable program or batch file."


If you got above error on your console then check you environment variable and add unpacked zend folder/bin to path variable now restart the console again and this error will not be longer visible.

Now you are ready for step two.


Step 2 - Create your project :-

Open a terminal (in Windows, Start -> Run, and then use cmd). Navigate to a directory where you would like to create a project. Then, use the path to the appropriate script, and execute following command:

"zf create project myproject"

after execution this command will create a folder myproject which structured like -

 
     myproject

      |-- .zfproject.xml
  
      |-- application
  
          |-- Bootstrap.php
 
          |-- configs
  
              |-- application.ini
 
          |-- controllers
 
              |-- ErrorController.php
  
              |-- IndexController.php
  
          |-- models
 
          |-- views
 
              |-- helpers
 
              |-- scripts
 
                  |-- error
 
                     |-- error.phtml
 
                  |-- index

                      |-- index.phtml
 
      |-- library
 
      |-- public

          |-- .htaccess
 
          |-- index.php
 
      |-- tests

          |-- application
 
              |-- bootstrap.php
 
          |-- library

              |-- bootstrap.php

          |-- phpunit.xml

      |-- public

          |-- .htaccess

Step two is done, check your peoject path and its structure.

Step 3 - Create a virtual host :-

(We are using Apache web server(http://httpd.apache.org/) for this example.)

To create your vhost, you need to know the location of your httpd.conf file, and potentially where other configuration files are located.

      C:\Program Files\Zend\Apache2\conf (Zend Server on Windows machines)

Define a virtual host in httpd.conf, you can use following reference if famalier with apache and php or just copy below

lines in httpd.conf

 
     <VirtualHost *:8181>

             ServerName localhost

             DocumentRoot "/path/to/myproject/public"       

           SetEnv APPLICATION_ENV "development"

             <Directory "/path/to/myproject/public">

                DirectoryIndex index.php

                AllowOverride All

                Order allow,deny

                Allow from all

           </Directory>

</VirtualHost>

- replace /path/to/ with your created myproject path

- Don't forget to add Listen 8181 in httpd.conf

- Rewrite module should enabled in httpd.conf.

- DocumentRoot setting specifies the public subdirectory of our project; this means that only files under that directory can ever be served directly by the server.

- AllowOverride, Order, and Allow directives; these are to allow us to use htacess files within our project.

- The SetEnv directive. What we are doing here is setting an environment variable for your virtual host; this variable will be picked up in the index.php and used to set the APPLICATION_ENV constant for our Zend Framework application. In production, you can omit this directive (in which case it will default to the value "production") or set it explicitly to "production".

Start your webserver (or restart it), and you should be ready to go.

Type http://localhost:8181/ on your browser and if everthing is gone fine you will got zend welcome page.

If you got issue "....(Zend/Application.php) [function.require-once]: failed to open stream...." then put the Zend folder (within you downloaded \ZendFramework-version-blah\library\ ) right in the application directory you are using with (looks like it is \path\to\myproject\lirary).

Hope it well helpful for all Zend framework beginners.