Zend - Installation And Sample Project Creation


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Compare Infobase Ltd
  • Zend - Installation And Sample Project Creation

    1. 1. Zend Framework Introduction by Susheel Kumar Sharma
    2. 2. Zend Framework Introduction <ul><li>Open Source Framework </li></ul><ul><li>100% Object-Oriented Code </li></ul><ul><li>Use-At-Will </li></ul><ul><li>  MVC Pattern </li></ul>
    3. 3. MVC <ul><li>Model - data, web services, feeds, etc. </li></ul><ul><li>View - The display returned to the user. </li></ul><ul><li>Controller - Manages the request environment, and determines what happens. </li></ul>
    4. 4. MVC Cont..
    5. 5. MVC Cont… <ul><li>Controller <-> View Controller and View can interact </li></ul><ul><li>Controller <-> Model Controller can pull data from the model for decisioning, or push data to the model </li></ul><ul><li>View <- Model View can access the model to retrieve data, but not write to it. </li></ul>
    6. 6. Installation Zend Framework Cont.. <ul><li>By Installing Zend Server </li></ul><ul><li>Manual Installation </li></ul>
    7. 7. Installation Zend Framework Cont.. <ul><li>Manual Installation </li></ul><ul><li>1.Check php is available from any directory </li></ul><ul><li>C:Userscompare>php –I </li></ul><ul><li>2.If not, set path for php.exe in System Environment Variable </li></ul><ul><li>a)Find your php installation directory - This can generally be found in one of the following places: </li></ul><ul><li>C:php </li></ul><ul><li>C:xamppphp </li></ul><ul><li>b)Go to -> My Computer -> (Right Click)Properties </li></ul><ul><li>-> Advanced -> Environment Variables -> System Variables -> (Select)Path -> Edit -> Variable Value(Paste) -> Ok </li></ul>
    8. 8. Installation Zend Framework Cont.. 3.Copy zf.bat, zf.php from ZendFrameworkbin and paste zf.bat, zf.php to C:php or C:xamppphp 4.Set up Zend Framework library to system PHP include_path a) Open php.ini in an editor b) Find include_path c) Add Zend Framework library e.g. include_path = &quot;.;G:xamppphppear;G:dataZendFrameworklibrary“ 5. Run command C:Userscompare>zf show version Zend Framework Version: 1.11.2
    9. 9. Create Project 1.Go to directory where you want to create project e.g. C:Userscompare>cd .. C:Users>cd .. C:>G: G:>cd xampphtdocs G:xampphtdocs> 2. Run command G:xampphtdocs> zf create project trainingtestproject Creating project at G:/xampp/htdocs/trainingtestproject Note: This command created a web project, for more information setting up your V HOST, please see docs/README
    10. 10. Create Project Cont.. 3. Project directory structure trainingtestproject |-- 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
    11. 11. Create Project Cont.. 4.Copy Zend directory from ZendFrameworklibraryZend and paste to trainingtestprojectlibraryZend(Optional)
    12. 12. Main Artifacts <ul><li>Bootstrap </li></ul><ul><li>Configuration </li></ul><ul><li>Action Controllers </li></ul><ul><li>Views </li></ul>
    13. 13. The Bootstrap <ul><li>Bootstrap class defines what resources and components to initialize </li></ul><ul><li>By default, Zend Framework's Front Controller is initialized </li></ul><ul><li>It uses the application/controllers/ as the default directory in which to look for action controllers </li></ul><ul><li>The class looks like the following: </li></ul><ul><li>// application/Bootstrap.php </li></ul><ul><li>class Bootstrap extends Zend_Application_Bootstrap_Bootstrap </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul>
    14. 14. Configuration <ul><li>Zend Framework is itself configuration less, need to configure application. </li></ul><ul><li>The default configuration is placed in application/configs/application.ini </li></ul><ul><li>Contains some basic directives for setting PHP environment  </li></ul><ul><li>It looks as follows: </li></ul>
    15. 15. Configuration Cont.. ; application/configs/application.ini [production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH &quot;/../library&quot; bootstrap.path = APPLICATION_PATH &quot;/Bootstrap.php&quot; bootstrap.class = &quot;Bootstrap&quot; appnamespace = &quot;Application&quot; resources.frontController.controllerDirectory = APPLICATION_PATH &quot;/controllers&quot; resources.frontController.params.displayExceptions = 0 [staging : production]   [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1
    16. 16. Action Controllers <ul><li>action controllers contain application workflow, do mapping requests to the appropriate models and views. </li></ul><ul><li>action controller should have one or more methods ending in “Action” </li></ul><ul><li>  methods may then be requested via the web </li></ul><ul><li>Zend Framework URLs follow the schema /controller/action </li></ul><ul><li>&quot;controller&quot; maps to the action controller name (minus the &quot;Controller&quot; suffix) and &quot;action&quot; maps to an action method (minus the &quot;Action&quot; suffix) </li></ul><ul><li>need an IndexController, which is a fallback controller and which also serves the home page of the site </li></ul><ul><li>ErrorController, which is used to indicate things such as HTTP 404 errors (controller or action not found) and HTTP 500 errors (application errors) </li></ul>
    17. 17. Action Controllers Cont.. <ul><li>The default IndexController is as follows: </li></ul><ul><li>// application/controllers/IndexController.php </li></ul><ul><li>class IndexController extends Zend_Controller_Action </li></ul><ul><li>{ </li></ul><ul><li>     public function init() </li></ul><ul><li>     { </li></ul><ul><li>         /* Initialize action controller here */ </li></ul><ul><li>     } </li></ul><ul><li>     public function indexAction() </li></ul><ul><li>     { </li></ul><ul><li>         // action body </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul>
    18. 18. Action Controllers Cont.. <ul><li>ErrorController is as follows: </li></ul><ul><li>// application/controllers/ErrorController.php </li></ul><ul><li>class ErrorController extends Zend_Controller_Action </li></ul><ul><li>{ </li></ul><ul><li>     public function errorAction() </li></ul><ul><li>     { </li></ul><ul><li>         $errors = $this->_getParam('error_handler'); </li></ul><ul><li>         switch ($errors->type) { </li></ul><ul><li>             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE: </li></ul><ul><li>             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLE </li></ul><ul><li>R: </li></ul><ul><li>            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: </li></ul><ul><li>                 // 404 error -- controller or action not found </li></ul><ul><li>                 $this->getResponse()->setHttpResponseCode(404); </li></ul><ul><li>                 $this->view->message = 'Page not found'; </li></ul><ul><li>                 break; </li></ul>
    19. 19.              default:                  // application error                  $this->getResponse()->setHttpResponseCode(500);                  $this->view->message = 'Application error';                  break;          }           $this->view->exception = $errors->exception;          $this->view->request   = $errors->request;      } }
    20. 20. Views <ul><li>Written in plain old PHP </li></ul><ul><li>View scripts are placed in application/views/scripts/ </li></ul><ul><li>They are further categorized using the controller names </li></ul><ul><li>We have an IndexController and an ErrorController </li></ul><ul><li>Thus we have corresponding  index/ and error/  </li></ul><ul><li>subdirectories within view scripts directory </li></ul><ul><li>We thus have the view scripts index/index.phtml  </li></ul><ul><li>and error/error.phtml </li></ul><ul><li>The following is what we install by default for the index/index.phtml view script: </li></ul>
    21. 21. Views Cont.. <!-- application/views/scripts/index/index.phtml --> <style>      a: link ,     a:visited{color: #0398CA;}     span#zf-name{color: #91BE3F;}     div#welcome{color: #FFFFFF;background-image: url(http://framework.zend.com/images/bkg_header.jpg); width:  600px;height: 400px;border: 2px solid #444444; overflow: hidden;text-align: center;}     div#more-information{background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);         height: 100%;}   </style> <div id=&quot;welcome&quot;>     <h1>Welcome to the <span id=&quot;zf-name&quot;>Zend Framework!</span><h1 />     <h3>This is your project's main page<h3 />
    22. 22.     <div id=&quot;more-information&quot;>         <p>             <img src=&quot;http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png&quot; />         </p>          <p>             Helpful Links: <br />             <a href=&quot;http://framework.zend.com/&quot;>Zend Framework Website</a> |             <a href=&quot;http://framework.zend.com/manual/en/&quot;>Zend Framework                 Manual</a>         </p>     </div> </div>
    23. 23. Views Cont.. <ul><li>The error/error.phtml view script : </li></ul><ul><li><!-- application/views/scripts/error/error.phtml --> </li></ul><ul><li><!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;; </li></ul><ul><li>     &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd> </li></ul><ul><li><html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;> </li></ul><ul><li><head> </li></ul><ul><li>  <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /> </li></ul><ul><li>  <title>Zend Framework Default Application</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li>  <h1>An error occurred</h1> </li></ul><ul><li>  <h2><?php  echo  $this->message ?></h2> </li></ul><ul><li>    <?php if ('development' == $this->env): ?> </li></ul><ul><li>   <h3>Exception information:</h3> </li></ul><ul><li>  <p> </li></ul><ul><li>  </li></ul>
    24. 24.     <b>Message:</b> <?php  echo  $this->exception->getMessage() ?>   </p>    <h3>Stack trace:</h3>   <pre><?php  echo  $this->exception->getTraceAsString() ?>   </pre>    <h3>Request Parameters:</h3>   <pre><?php  echo   var _export ($this->request->getParams(), 1) ?>   </pre>    <?php endif ?>   </body> </html>
    25. 25. Create a virtual host(Optional) <ul><li>Open httpd.conf file in any editor, </li></ul><ul><li>Some common locations: /etc/httpd/httpd.conf (Fedora, RHEL, and others), C:Program FilesZendApache2conf (Zend Server on Windows machines)/ G:xamppapacheconf </li></ul><ul><li>  First, ensure that the NameVirtualHost is defined; set it to a value of &quot;*:80&quot; </li></ul><ul><li>define a virtual host: </li></ul><ul><li><VirtualHost *:80> </li></ul><ul><li>     ServerName quickstart.local </li></ul><ul><li>     DocumentRoot /path/to/quickstart/public </li></ul><ul><li>     SetEnv APPLICATION_ENV &quot;development&quot; </li></ul><ul><li>    <Directory /path/to/quickstart/public> </li></ul><ul><li>         DirectoryIndex index.php </li></ul><ul><li>         AllowOverride All </li></ul><ul><li>         Order allow,deny </li></ul><ul><li>         Allow from all </li></ul><ul><li>    </Directory> </li></ul><ul><li></VirtualHost> </li></ul>
    26. 26. Create a virtual host(Optional) Cont.. <VirtualHost *:80>      ServerName  trainingtestproject.local      DocumentRoot /path/to/quickstart/public      SetEnv APPLICATION_ENV &quot;development&quot;     <Directory /path/to/quickstart/public>          DirectoryIndex index.php          AllowOverride All          Order allow,deny          Allow from all     </Directory> </VirtualHost>
    27. 27. Create a virtual host(Optional) Cont.. <ul><li>  DocumentRoot setting specifies the public subdirectory of project; this means that only files under that directory can ever be served directly by the server </li></ul><ul><li>AllowOverride, Order, and Allow directives; these are to allow to use htacess files within project </li></ul><ul><li>SetEnv directive is setting an environment variable for 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 </li></ul><ul><li>Add an entry in hosts file located at : On *nix-like systems, this is usually /etc/hosts; </li></ul><ul><li>On windows : C:WINDOWSsystem32driversetc </li></ul><ul><li> trainingtestproject.local </li></ul>
    28. 28. Reference : http://framework.zend.com/ http://framework.zend.com/manual/en/