Get Started with Zend Framework 2

74
Get started with Zend Framework 2 Presenter : Pravasini Sahoo, Mindfire Solutions Date: 12 th August 2014

description

Zend Framework 2 (ZF2) is an open source, object oriented Framework designed for developing web applications and services using PHP 5.3+. This presentation covers an overview on Zend 2, advantages of using this and installation process.

Transcript of Get Started with Zend Framework 2

Page 1: Get Started with Zend Framework 2

Get started with

Zend Framework 2Presenter :

Pravasini Sahoo, Mindfire SolutionsDate: 12th August 2014

Page 2: Get Started with Zend Framework 2

About Me

2 | 12 Aug 2014

Pravasini Sahoo

• Zend Certified Engineer (ZCE)Zend PHP 5.3 Certification

• Oracle Certified ProfessionalOCP MySQL 5 Developer - Part 1 - 1Z0-871

• Microsoft Certified ProfessionalMCP – Programming in HTML5 with JavaScript and CSS3 – MCTS 70-480

“ Working as a PHP Developer in MFS for around 4years. I started my professional career at Mindfire on 2010. I had done some projects using Zend 1.12 version and some using Zend 2. My skill is on PHP, MySQL, CSS, CSS3, HTML5, JQuery, AJAX, JavaScript, XML, ZendFramework, GoogleAnalytics, HTML , FacebookAPI, YouTubeAPI, OCP1, Znedframework2, TwitterBootstrap, JSON “

Certifications

Page 3: Get Started with Zend Framework 2

Agenda1. Introduction to Framework

2. What is MVC?

3. Why to use Zend Framework 2?

4. Zend2 Installation

5. Control Flow

6. Demo on a CRUD application

3 | 12 Aug 2014

Page 4: Get Started with Zend Framework 2

Introduction to Framework

4 | 12 Aug 2014

Page 5: Get Started with Zend Framework 2

1. Introduction to Framework

“A Framework is a universal, reusable software platform to develop software applications, products and solutions. In other words, we can say it is some kind of library, a piece of software, which provide web developers with code base and consistent standardized ways of creating web applications”.

5 | 12 Aug 2014

Page 6: Get Started with Zend Framework 2

Benefits of using Framework1. Code and File Organization2. The MVC Pattern3. Enforcing of Good Coding Standards4. Utilities and Libraries5. Less Code & Faster Development6. Community Support7. Security8. Performance Tools9. Simplified and Pretty URLs10. Efficiently Access Database ... and lot more

6 | 12 Aug 2014

Page 7: Get Started with Zend Framework 2

What is MVC?

7 | 12 Aug 2014

Page 8: Get Started with Zend Framework 2

2. What is MVC?MVC is a software architecture - the structure of the system - that separates domain/application/business (whatever you prefer) logic from the rest of the user interface. It does this by separating the application into three parts: the model, the view, and the controller.

The controller receives user input and makes calls to model objects and the view to perform appropriate actions.

The model manages fundamental behaviors and data of the application.

The view effectively provides the user interface element of the application.

8 | 12 Aug 2014

Page 9: Get Started with Zend Framework 2

An XYZ Company Hierarchy

DEPARTMENT ONE DEPARTMENT TWO

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

9 | 12 Aug 2014

Page 10: Get Started with Zend Framework 2

Customer arrives….

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

10 | 12 Aug 2014

DEPARTMENT ONE DEPARTMENT TWO

Page 11: Get Started with Zend Framework 2

Information desk greets him/her

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

11 | 12 Aug 2014

Page 12: Get Started with Zend Framework 2

..gives him/her directions..

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

12 | 12 Aug 2014

Page 13: Get Started with Zend Framework 2

Warehouse folks do things

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

13 | 12 Aug 2014

Page 14: Get Started with Zend Framework 2

Stuff is ready for delivery

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

14 | 12 Aug 2014

Page 15: Get Started with Zend Framework 2

Customer is happy.

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

15 | 12 Aug 2014

Page 16: Get Started with Zend Framework 2

Why we are checking company structure ?

16 | 12 Aug 2014

Page 17: Get Started with Zend Framework 2

This is MVC !17 | 12 Aug 2014

Why we are checking company structure ?

Page 18: Get Started with Zend Framework 2

http://framework.zend.com/

18 | 12 Aug 2014

Page 19: Get Started with Zend Framework 2

Our Application

VIEW VIEW

CONTROLLER CONTROLLER

MODEL19 | 12 Aug 2014

Page 20: Get Started with Zend Framework 2

Back to the XYZ company…

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

20 | 12 Aug 2014

Page 21: Get Started with Zend Framework 2

Why to use Framework 2?

21 | 12 Aug 2014

Page 22: Get Started with Zend Framework 2

We need to move a department..

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

22 | 12 Aug 2014

Page 23: Get Started with Zend Framework 2

Dependency problem

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

23 | 12 Aug 2014

Page 24: Get Started with Zend Framework 2

In our Application

VIEW VIEW

CONTROLLER CONTROLLER

MODEL24 | 12 Aug 2014

Page 25: Get Started with Zend Framework 2

Same problem

VIEW VIEW

CONTROLLER CONTROLLER

MODEL25 | 12 Aug 2014

Page 26: Get Started with Zend Framework 2

Solving company problem

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

WAREHOUSE

26 | 12 Aug 2014

Page 27: Get Started with Zend Framework 2

Same with our application

VIEW VIEW

CONTROLLER CONTROLLER

MODEL MODEL

27 | 12 Aug 2014

Page 28: Get Started with Zend Framework 2

Project delivered !

VIEWCONTROLLER

MODEL

VIEWCONTROLLER

MODEL

28 | 12 Aug 2014

Page 29: Get Started with Zend Framework 2

Let’s start a new one…

VIEWCONTROLLER

MODEL

VIEWCONTROLLER

MODEL

29 | 12 Aug 2014

Page 30: Get Started with Zend Framework 2

Hey ! We need this thing

VIEWCONTROLLER

MODEL

VIEWCONTROLLER

MODEL

30 | 12 Aug 2014

Page 31: Get Started with Zend Framework 2

Problem Solved !

VIEWCONTROLLER

MODEL

VIEWCONTROLLER

MODELVIEW

CONTROLLER

MODEL

31 | 12 Aug 2014

Page 32: Get Started with Zend Framework 2

http://framework.zend.com/

32 | 12 Aug 2014

Page 33: Get Started with Zend Framework 2

Namespace + Module.php = Module

33 | 12 Aug 2014

Page 34: Get Started with Zend Framework 2

Namespace + Module.php = Module

Simplest Module :

Namespace Album;

Class Module { }

34 | 12 Aug 2014

Page 35: Get Started with Zend Framework 2

What’s in a Module ?

Feature

35 | 12 Aug 2014

Page 36: Get Started with Zend Framework 2

What’s in a Module ?

Feature

Routing

Controller

View

PHP Classes

Configuration

. . .

36 | 12 Aug 2014

Page 37: Get Started with Zend Framework 2

Business Logic Hints

Feature

Routing

Controller

View

PHP Classes

Configuration

. . .

Services

Helpers/ Plugins

Mappers

. . .

37 | 12 Aug 2014

Page 38: Get Started with Zend Framework 2

Zend2 Installation

38 | 12 Aug 2014

Page 39: Get Started with Zend Framework 2

Cloning

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

39 | 12 Aug 2014

Page 40: Get Started with Zend Framework 2

Composer.json{ "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause", "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.3.*" }}

40 | 12 Aug 2014

Page 41: Get Started with Zend Framework 2

Composer.json{ "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause", "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.3.*" }}

41 | 12 Aug 2014

Page 42: Get Started with Zend Framework 2

Installation

cd ZendSkeletonApplication

php composer.phar self-update php composer.phar install

42 | 12 Aug 2014

Page 43: Get Started with Zend Framework 2

Installation

cd ZendSkeletonApplication

php composer.phar self-update php composer.phar install> Installing zendframework/zendframework

43 | 12 Aug 2014

Page 44: Get Started with Zend Framework 2

File System Layout

44 | 12 Aug 2014

Page 45: Get Started with Zend Framework 2

File System Layout

Application wide configuration

45 | 12 Aug 2014

Page 46: Get Started with Zend Framework 2

File System Layout

document, cached file, coding standard and guidelines

46 | 12 Aug 2014

Page 47: Get Started with Zend Framework 2

File System Layout

Public Document Root

47 | 12 Aug 2014

Page 48: Get Started with Zend Framework 2

File System Layout

Our Modules

48 | 12 Aug 2014

Page 49: Get Started with Zend Framework 2

File System Layout

Third Party Modules}49 | 12 Aug 2014

Page 50: Get Started with Zend Framework 2

Focusing on a module…

Module specific configuration

50 | 12 Aug 2014

Page 51: Get Started with Zend Framework 2

What’s inside a module ?

Module PHP Source Files(Controller, Form, Model, Services …)

51 | 12 Aug 2014

Page 52: Get Started with Zend Framework 2

What’s inside a module ?

Module test files

52 | 12 Aug 2014

Page 53: Get Started with Zend Framework 2

What’s inside a module ?

View Stuff(view files, layouts, partials)

53 | 12 Aug 2014

Page 54: Get Started with Zend Framework 2

Control Flow

54 | 12 Aug 2014

Page 55: Get Started with Zend Framework 2

index.php<?php/** * This makes our life easier when dealing with paths. Everything is relative * to the application root now. */chdir(dirname(__DIR__));

// Decline static file requests back to the PHP built-in webserverif (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) { return false;}

// Setup autoloadingrequire 'init_autoloader.php';

// Run the application!Zend\Mvc\Application::init(require 'config/application.config.php')->run();

55 | 12 Aug 2014

Page 56: Get Started with Zend Framework 2

index.php

56 | 12 Aug 2014

<?php/** * This makes our life easier when dealing with paths. Everything is relative * to the application root now. */chdir(dirname(__DIR__));

// Decline static file requests back to the PHP built-in webserverif (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) { return false;}

// Setup autoloadingrequire 'init_autoloader.php';

// Run the application!Zend\Mvc\Application::init(require 'config/application.config.php')->run();

Page 57: Get Started with Zend Framework 2

Config/application.config.php<?phpreturn array( // This should be an array of module namespaces used in the application. 'modules' => array( 'Application', 'Album' ),

// These are various options for the listeners attached to the ModuleManager 'module_listener_options' => array( 'module_paths' => array( './module', './vendor', ), 'config_glob_paths' => array( 'config/autoload/{,*.}{global,local}.php', ) ));

57 | 12 Aug 2014

Page 58: Get Started with Zend Framework 2

Module.phpnamespace Application; use Zend\Mvc\ModuleRouteListener; use Zend\Mvc\MvcEvent;

class Module { public function onBootstrap(MvcEvent $e) { ... } public function getConfig() { return include __DIR__ . '/config/module.config.php'; } public function getAutoloaderConfig() { ... } }

58 | 12 Aug 2014

Page 59: Get Started with Zend Framework 2

Module configuration file// config/module.config.php

return array(

'router' => array(

'routes' => array(...)

),

'controllers' => array(...),

'service_manager' => array(...),

'view_manager' => array(...),

...

)

59 | 12 Aug 2014

Page 60: Get Started with Zend Framework 2

Controllers

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController{ public function indexAction() { return new ViewModel(); }}

60 | 12 Aug 2014

Page 61: Get Started with Zend Framework 2

Actions

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController { public function indexAction() {

return new ViewModel(); } }

61 | 12 Aug 2014

Page 62: Get Started with Zend Framework 2

Routing

Controller

Views

PHP Classes

Configurations

. . .

Feature A

62 | 12 Aug 2014

Page 63: Get Started with Zend Framework 2

Routing

Controller

Views

PHP Classes

Configurations

. . .

Feature A

Routing

Controller

Views

PHP Classes

Configurations

. . .

Feature B

Routing

Controller

Views

PHP Classes

Configurations

. . .

Feature C

63 | 12 Aug 2014

Page 64: Get Started with Zend Framework 2

Routing

Controller

Views

PHP Classes

Configurations

. . .

Routing

Controller

Views

PHP Classes

Configurations

. . .

Routing

Controller

Views

PHP Classes

Configurations

. . .

Modules are merged at runtime

64 | 12 Aug 2014

Page 65: Get Started with Zend Framework 2

Routing

Controller

Views

PHP Classes

Configurations

. . .

Routing

Controller

Views

PHP Classes

Configurations

. . .

Routing

Controller

Views

PHP Classes

Configurations

. . .

Modules are merged at runtime

Configurations

65 | 12 Aug 2014

Page 66: Get Started with Zend Framework 2

We can override things…

Views / Layouts

All Other Configuration Parameters

Routing Rules

66 | 12 Aug 2014

Page 67: Get Started with Zend Framework 2

We can override things

Views / Layouts

All Other Configuration Parameters

Routing Rules }Do NOT modify original 3rd party

modules! 67 | 12 Aug 2014

Page 68: Get Started with Zend Framework 2

Custom configuration

} Do not modify anything from here !

68 | 12 Aug 2014

Page 69: Get Started with Zend Framework 2

Modules are merged at runtime

Routing

Controller

Views

PHP Classes

Configurations

. . .

Routing

Controller

Views

PHP Classes

Configurations

. . .

Routing

Controller

Views

PHP Classes

Configurations

. . .

69 | 12 Aug 2014

Page 70: Get Started with Zend Framework 2

This happens for all modules

config/application.config.php

return array(

'modules' => array(

'DoctrineModule',

'DoctrineORMModule',

'Application',

),

),

...

70 | 12 Aug 2014

} Order is important

Page 71: Get Started with Zend Framework 2

71 | 12 Aug 2014

Demo on a CRUD application

Page 72: Get Started with Zend Framework 2

References1. http://framework.zend.com/manual/2.0/en/ref/overview.html

2. http://framework.zend.com/manual/2.0/en/user-guide/modules.html

3. http://framework.zend.com/about/faq/

4. http://www.slideshare.net/maraspin/zf2-modular-architecture-taking-advantage-of-it

5. http://blog.hock.in/2012/09/06/zf2-for-zf1-users-part-1/

6. http://stackoverflow.com/questions/12314300/zend-framework-1-vs-zend-framework-2-performance

72 | 12 Aug 2014

Page 73: Get Started with Zend Framework 2

73 | 12 Aug 2014

Q & A

Page 74: Get Started with Zend Framework 2

74 | 12 Aug 2014

Thank You !