Get Started with Zend Framework 2

Post on 27-Nov-2014

249 views 1 download

Tags:

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

Get started with

Zend Framework 2Presenter :

Pravasini Sahoo, Mindfire SolutionsDate: 12th August 2014

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

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

Introduction to Framework

4 | 12 Aug 2014

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

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

What is MVC?

7 | 12 Aug 2014

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

An XYZ Company Hierarchy

DEPARTMENT ONE DEPARTMENT TWO

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

9 | 12 Aug 2014

Customer arrives….

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

10 | 12 Aug 2014

DEPARTMENT ONE DEPARTMENT TWO

Information desk greets him/her

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

11 | 12 Aug 2014

..gives him/her directions..

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

12 | 12 Aug 2014

Warehouse folks do things

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

13 | 12 Aug 2014

Stuff is ready for delivery

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

14 | 12 Aug 2014

Customer is happy.

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

15 | 12 Aug 2014

Why we are checking company structure ?

16 | 12 Aug 2014

This is MVC !17 | 12 Aug 2014

Why we are checking company structure ?

http://framework.zend.com/

18 | 12 Aug 2014

Our Application

VIEW VIEW

CONTROLLER CONTROLLER

MODEL19 | 12 Aug 2014

Back to the XYZ company…

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

20 | 12 Aug 2014

Why to use Framework 2?

21 | 12 Aug 2014

We need to move a department..

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

22 | 12 Aug 2014

Dependency problem

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

23 | 12 Aug 2014

In our Application

VIEW VIEW

CONTROLLER CONTROLLER

MODEL24 | 12 Aug 2014

Same problem

VIEW VIEW

CONTROLLER CONTROLLER

MODEL25 | 12 Aug 2014

Solving company problem

ACCOUNTS MANAGER ACCOUNTS MANAGER

TECHNICAL OFFICE TECHNICAL OFFICE

WAREHOUSE

INFORMATIONDESK

WAREHOUSE

26 | 12 Aug 2014

Same with our application

VIEW VIEW

CONTROLLER CONTROLLER

MODEL MODEL

27 | 12 Aug 2014

Project delivered !

VIEWCONTROLLER

MODEL

VIEWCONTROLLER

MODEL

28 | 12 Aug 2014

Let’s start a new one…

VIEWCONTROLLER

MODEL

VIEWCONTROLLER

MODEL

29 | 12 Aug 2014

Hey ! We need this thing

VIEWCONTROLLER

MODEL

VIEWCONTROLLER

MODEL

30 | 12 Aug 2014

Problem Solved !

VIEWCONTROLLER

MODEL

VIEWCONTROLLER

MODELVIEW

CONTROLLER

MODEL

31 | 12 Aug 2014

http://framework.zend.com/

32 | 12 Aug 2014

Namespace + Module.php = Module

33 | 12 Aug 2014

Namespace + Module.php = Module

Simplest Module :

Namespace Album;

Class Module { }

34 | 12 Aug 2014

What’s in a Module ?

Feature

35 | 12 Aug 2014

What’s in a Module ?

Feature

Routing

Controller

View

PHP Classes

Configuration

. . .

36 | 12 Aug 2014

Business Logic Hints

Feature

Routing

Controller

View

PHP Classes

Configuration

. . .

Services

Helpers/ Plugins

Mappers

. . .

37 | 12 Aug 2014

Zend2 Installation

38 | 12 Aug 2014

Cloning

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

39 | 12 Aug 2014

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

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

Installation

cd ZendSkeletonApplication

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

42 | 12 Aug 2014

Installation

cd ZendSkeletonApplication

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

43 | 12 Aug 2014

File System Layout

44 | 12 Aug 2014

File System Layout

Application wide configuration

45 | 12 Aug 2014

File System Layout

document, cached file, coding standard and guidelines

46 | 12 Aug 2014

File System Layout

Public Document Root

47 | 12 Aug 2014

File System Layout

Our Modules

48 | 12 Aug 2014

File System Layout

Third Party Modules}49 | 12 Aug 2014

Focusing on a module…

Module specific configuration

50 | 12 Aug 2014

What’s inside a module ?

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

51 | 12 Aug 2014

What’s inside a module ?

Module test files

52 | 12 Aug 2014

What’s inside a module ?

View Stuff(view files, layouts, partials)

53 | 12 Aug 2014

Control Flow

54 | 12 Aug 2014

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

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();

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

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

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

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

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

Routing

Controller

Views

PHP Classes

Configurations

. . .

Feature A

62 | 12 Aug 2014

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

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

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

We can override things…

Views / Layouts

All Other Configuration Parameters

Routing Rules

66 | 12 Aug 2014

We can override things

Views / Layouts

All Other Configuration Parameters

Routing Rules }Do NOT modify original 3rd party

modules! 67 | 12 Aug 2014

Custom configuration

} Do not modify anything from here !

68 | 12 Aug 2014

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

This happens for all modules

config/application.config.php

return array(

'modules' => array(

'DoctrineModule',

'DoctrineORMModule',

'Application',

),

),

...

70 | 12 Aug 2014

} Order is important

71 | 12 Aug 2014

Demo on a CRUD application

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

73 | 12 Aug 2014

Q & A

74 | 12 Aug 2014

Thank You !