15
Introduction to Zend_Controller and Actions with Paramters

Hebrew, Introduction to Zend Controller And new technique

Embed Size (px)

DESCRIPTION

1. Introduction to Zend Controller from Zend Framework. 2. Action with parameters passed as function's arguments.

Citation preview

Page 1: Hebrew, Introduction to Zend Controller And new technique

Introduction to Zend_Controllerand

Actions with Paramters

Page 2: Hebrew, Introduction to Zend Controller And new technique

?מי אני

ממודיעין17.5 בן , ניר טייב.

כיום. שנים בטכנולוגיות שונות3-4 מתכנת לאינטרנט מזה-Rails.וRuby בתור מתכנת -Kontera.com עובד ב

בלוגר – כותב על בניית UIב ,JavaScript-צד שרת וחדשות. http://webdev.blogli.co.ilבתחום

מתכנת ב PHP-כשנתיים ומשתמש ב ZF-חודשים-3 כ.

Page 3: Hebrew, Introduction to Zend Controller And new technique

,Zend Frameworkמה זה?

מכילה רכיבים שונים למטרות שונות- ספריית רכיבים,.שלא תלויים אחד בשני, בסיסיות ומבוקשות

ב100% כתוב PHP 5.1.4-בלי להתבסס על שום.Extensions

משוחרר. קוד פתוח – כל אחד יכול לתרום לפריימוורקBSD Like.ברישיון

Page 4: Hebrew, Introduction to Zend Controller And new technique

Zend_Controllerמה זה?

חלק מרכיב ה MVC-של ה ,Framework-משמש ליצירת ה- Controllerה. באפליקצייה C-ב.MVC-

משמש לניתור בקשת ה HTTP-ומחליט מה עושים איתה. לפי הנתונים שנשלחו יחד איתה

Page 5: Hebrew, Introduction to Zend Controller And new technique

,Zend_Controllerבסיס.

יורש את.Zend_Controller_Action

שם המחלקה יסתיים בController-IndexController, BlogController

קו תחתון בשם המחלקה מסמן חלוקה לתיקיות:Foo_AdminController =< Foo/AdminController.php

מותר להשתמש ב ,CamelCase-שם הקובץ יכיל מקף או.נקודה בסוף כל מילה

Page 6: Hebrew, Introduction to Zend Controller And new technique

Controller Actions:

– Actionמתודה ב Controller-לניתור בקשת הHTTP.-http://domain.com/entries/list =< Action: list

כל מתודה שמסתיימת ב “Action“-היא חשופה לבחוץ.public function listAction

Page 7: Hebrew, Introduction to Zend Controller And new technique

Front Controller

מטפל בכל הבקשות.מדביק את האפליקציה ביחד.מחזיר תשובה ללקוח.

Page 8: Hebrew, Introduction to Zend Controller And new technique

?איך הכל עובד

Page 9: Hebrew, Introduction to Zend Controller And new technique

קצת קוד

class EntriesController extens Zend_Controller_Action{

public function indexAction(){echo 'Index !';

}

public function listAction(){echo '1, 2, 3';

}

}

Page 10: Hebrew, Introduction to Zend Controller And new technique

-Post Dataוב-QueryString טיפול ב

המתודה _getParamלקריאה למשתנה אחד .

המתודה _getAllParamsלקריאה של כולם.

המתודות מקבלות את כל המשתנים מה - QueryString-שורת-המשתנים שנשלחים בגוף בקשת ה-Post Data - ומה, הכתובת ,HTTP לרוב בעזרת טופס עם.method=post

Page 11: Hebrew, Introduction to Zend Controller And new technique

ובדרך אחרת

להשתמש בפרמטרים של ה Actions-לגישה לQueryString--Post Data.וה

יתרונות:רשימה ברורה של כל הפרמטרים*

.type hinting *.ערכי ברירת מחדל*

ביצועים: חסרונות.

Page 12: Hebrew, Introduction to Zend Controller And new technique

:דוגמאות Framework Way:

public function listAction(){

$entry = $model-<find(intval($this-<_getParam(”id”)));

$mode = @$this-<_getParam(”mode”);

echo showContent($mode || 'plain', $entry-<data);

}

New Way:public function listAction($id, $mode='plain'){

$entry = $model-<find(intval($id));

echo showContent($mode, $content-<data);

}

Page 13: Hebrew, Introduction to Zend Controller And new technique

?איך עושים זאת

בשישה שלבים:.Zend_Controller_Actionיורשים את 1. .dispatchדורסים את המתודה 2. .במערך-Request Parameters שומרים את כל ה3. .במערך-Method Parameters שומרים את כל ה4. בסדר3 דינמית תוך כדי מיפוי המערך משלב -Action מפעילים את ה5.

.4של שלב 1.באפליקציה יירש מהמחלקה שיצרנו בשלב Controller כל 6.

Page 14: Hebrew, Introduction to Zend Controller And new technique

:מימוש התהליך// 1. Make a new controller class the inherit Zend_Controller_Actionclass Action_With_Parameters_Controller extends Zend_Controller_Action {

// 2. Override the `dispatch` methodpublic function dispatch($action) {

// 3. Get all request parameters$params = $this-<_getAllParams();

// 4. Get all action method parameters$method_params_array = $this-<get_action_params($action);

$data = array(); // It will sent to the action

foreach($method_params_array as $param) {$name = $param-<getName();if($param-<isOptional()) { // Check whether the parameter is optional

// If there is no data to send, use the default$data[$name] = !empty($params[$name])? $params[$name] : $param-<getDefaultValue();

} elseif(empty($params[$name])) {// The parameter cannot be empty as definedthrow new Exception('Parameter: '.$name.' Cannot be empty');

} else {$data[$name] = $params[$name];

}}

// 5. Invoke the action and pass the request parameters as actions method parameters, according to their order and names.call_user_func_array(array($this, $action), $data);

}

private function get_action_params($action) {$classRef = new ReflectionObject($this);$className = $classRef-<getName();$funcRef = new ReflectionMethod($className, $action);$paramsRef = $funcRef-<getParameters();return $paramsRef;

}}

Page 15: Hebrew, Introduction to Zend Controller And new technique

!תודה

לעוד חומר בנושא:

● http://framework.zend.com/manual/en/zend.controller.html:כתובה במאמר מפורט, השיטה המוצגת כאן ●

http://devzone.zend.com/article/2855.לקרוא את התגובות! חשוב מאוד

Zend Framework:של -MVC מצגת מקיפה על רכיב ה ●

http://devzone.zend.com/content/zendcon_07_slides/Ophinney_Matthew_2007-ZendCon-MVC.pdf