1 / 61

Technologie tworzenia aplikacji internetowych Wykład 4

dr inż. Piotr Czapiewski. Technologie tworzenia aplikacji internetowych Wykład 4. PHP – Member overloading. PHP – Member overloading. Overloading Znaczenie inne niż w większości języków obiektowych Metoda dynamicznego tworzenia pól i metod „Magiczne metody”

senwe
Télécharger la présentation

Technologie tworzenia aplikacji internetowych Wykład 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. dr inż. Piotr Czapiewski Technologie tworzenia aplikacji internetowychWykład 4

  2. PHP – Memberoverloading

  3. PHP – Memberoverloading Overloading • Znaczenie inne niż w większości języków obiektowych • Metoda dynamicznego tworzenia pól i metod „Magiczne metody” • __get(), __set(), __isset(), __unset() • Wywoływane automatycznie przy próbie dostępu do nieistniejącego lub ukrytego pola (private, protected)

  4. Przykłady stosowania w Zend • Przekazywanie danych z kontrolera do widoku • Przechowywanie danych w modelu • Równocześnie dostęp obiektowyi łatwy zapis do bazy

  5. Zend Framework Przykład – Kalkulator

  6. Nowy projekt – Kalkulator zfcreateproject Kalkulator cd Kalkulator

  7. Konfiguracja hosta wirtualnego • Konfiguracja serwera Apache • Plik httpd.conf lub plik dołączany przez httpd.conf • W przypadku pakietu XAMPP: c:\xampp\apache\conf\extra\httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/Kalkulator/public" ServerNamekalkulator.localhost ErrorLog "logs/kalkulator.error.log" CustomLog "logs/kalkulator.access.log" combined </VirtualHost>

  8. Konfiguracja hosta wirtualnego • Konfiguracja systemu • Jaki adres IP ma host o nazwie kalkulator.localhost? • Plik hosts: c:\windows\system32\drivers\etc\hosts 127.0.0.1 kalkulator.localhost

  9. http://kalkulator.localhost

  10. Widok – akcja index/views/scripts/index/index.phtml <div><h1>Witamy w Zendowym Kalkulatorze</h1><formaction="/index/dodaj"method="POST"><inputtype="text"name="a"><inputtype="text"name="b"><inputtype="submit"value="Dodaj"></form></div>

  11. Nowa akcja – dodaj zf create action dodaj Index

  12. Kontroler – akcja dodajIndexController.php, dodajAction() publicfunctiondodajAction(){$a = $this->getRequest()->getParam('a');$b = $this->getRequest()->getParam('b'); $c = $a + $b;$this->view->wynik = $c;}

  13. Widok – akcja dodaj/views/scripts/index/dodaj.phtml <h1>Wynik dodawania:<?phpecho$this->wynik;?></h1><ahref="/index/index/"> Fajnie było, chcę jeszcze raz! </a>

  14. Kalkulator w akcji

  15. Zend Framework Zend_Layout, Zend_Db, Zend_Form

  16. Zend_Layout

  17. Włączenie Zend_Layout zfenablelayout [production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" appnamespace = "Application" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.params.displayExceptions = 0 resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

  18. Zend_Layout <html><head><title>Kalkulator</title><metahttp-equiv="content-type“content="text/html; charset=UTF-8"></head><body><?phpecho$this->layout()->content?></body></html>

  19. Zend_Layout Szablon: layout.phtml <html><head><title>Kalkulator</title><metahttp-equiv="content-type“content="text/html; charset=UTF-8"></head><body><?phpecho$this->layout()->content?></body></html> Kod wysłany do przeglądarki <html><head><title>Kalkulator</title><metahttp-equiv="content-type„content="text/html; charset=UTF-8"></head><body><div><h1>Witamy w Zendowym Kalkulatorze</h1><formaction="/index/dodaj"method="POST"><inputtype="text"name="a"><inputtype="text"name="b"><inputtype="submit"value="Dodaj"></form></div></body></html> Widok związany z akcją: index.phtml <div><h1>Witamy w Zendowym Kalkulatorze</h1><formaction="/index/dodaj"method="POST"><inputtype="text"name="a"><inputtype="text"name="b"><inputtype="submit"value="Dodaj"></form></div>

  20. Zend_Form classApplication_Form_KalkulatorextendsZend_Form{ publicfunctioninit() {$this->setMethod('post');$this->addElement('text', 'a');$this->addElement('text', 'b');$this->addElement('submit', 'Dodaj'); } }

  21. Zend_Form publicfunctioninit() {$this->setMethod('post');$a = $this->createElement('text', 'a');$a->setRequired(true);$a->addValidator(newZend_Validate_Int());$a->setLabel('a = ');$this->addElement($a);$b = $this->createElement('text', 'b');$b->setRequired(true);$b->addValidator(newZend_Validate_Int());$b->setLabel('b = ');$this->addElement($b);$this->addElement('submit', 'Dodaj');}

  22. Zend_Form publicfunctioninit() {$this->setMethod('post');$this->addElement('text', 'a', array('label' => 'a = ','required' => true,'validators' => array('int') ));$this->addElement('text', 'b', array('label' => 'b = ','required' => true,'validators' => array('int') ));$this->addElement('submit', 'Dodaj');}

  23. Zend_Form – wyświetlanie formularza Kontroler: IndexController.php publicfunctionindexAction() {$form = newApplication_Form_Kalkulator();$form->setAction('/index/index');$this->view->form = $form;} Widok: index.phtml <div><h1>Witamy w Zendowym Kalkulatorze</h1><?phpecho$this->form; ?></div>

  24. Zend_Form – obsługa formularza publicfunctionindexAction() {$form = newApplication_Form_Kalkulator();$form->setAction('/index/index');$this->view->form = $form;if($this->getRequest()->isPost()) {if($form->isValid($_POST)) {$this->_forward('dodaj'); } }}

  25. Zend_DbKonfiguracja dostępu do bazy danych [production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" appnamespace = "Application" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.params.displayExceptions = 0 resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" resources.db.adapter = "pdo_mysql" resources.db.params.host = "localhost" resources.db.params.username = "student99" resources.db.params.password = "tajne_haslo" resources.db.params.dbname = "student99" resources.db.params.charset = utf8 [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 resources.frontController.params.displayExceptions = 1 resources.db.adapter = "pdo_mysql" resources.db.params.host = "localhost" resources.db.params.username = "student99" resources.db.params.password = "tajne_haslo" resources.db.params.dbname = "student99" resources.db.params.charset = utf8

  26. Zend_DbZapytania SQL • Dostęp do adaptera bazy danych: • $db = Zend_Db_Table::getDefaultAdapter(); • Metody dostępu do danych: • insert(), update(), delete(), query(), … • fetchAll(), fetchAssoc(), fetchCol(), fetchOne(), …

  27. Zend_DbZapytania SQL - przykłady $db = Zend_Db_Table::getDefaultAdapter();$sql = 'select * from wpisy';$result = $db->fetchAll($sql);foreach($resultas$row)echo$row['id'] . ' ' . $row['tytul'] . '<br>'; Zwraca tablicę $sql = 'select * from wpisy where id = ?';$row = $db->fetchRow($sql, array(1));echo$row['id'] . ' ' . $row['tytul'] . '<br>';

  28. Zend_DbZapytania SQL - przykłady $sql = 'select id, tytulfrom wpisy';$result = $db->fetchPairs($sql);foreach($resultas$id => $tytul)echo$id . ' ' . $tytul . '<br>'; $sql = 'selectcount(*) from wpisy';$n = $db->fetchOne($sql);

  29. Zend_Db_Table • Realizacja wzorca projektowego Table Data Gateway • Tworzenie klasy pośredniczącej w dostępie do tabeli bazy danych • Jedna tabela = jedna klasa pośrednicząca • Proste operacje – bez użycia SQL • Metody: • fetchAll(), find(), save(), delete(), insert(), createRow()

  30. Zend_Db_Table Tabela w bazie danych: wpisy Model: Wpisy.php <?phpclassApplication_Model_WpisyextendsZend_Db_Table_Abstract{protected$_name = 'wpisy';}

  31. Zend_Db_TablePobieranie danych $model = newApplication_Model_Wpisy();$wpisy = $model->fetchAll();foreach($wpisyas$wpis)echo$wpis->tytul . '<br>'; Zwraca obiekt klasy Zend_Db_Table_Rowset_Abstract $res = $model->fetchAll('id < 7', 'tytul'); $res = $model->fetchAll('id < 7', 'tytul'); $wpis = $model->find(3)->current();

  32. Zend_Db_TableDodawanie nowych wierszy $dane = array('tytul' => 'Nowy wpis','tresc' => 'Ala ma kota...','skrot' => 'Ala...' ); $model->insert($dane); Metoda insert $wpis = $model->createRow();$wpis->tytul = 'Nowy wpis';$wpis->tresc = 'Ala ma kota...';$wpis->skrot = 'Ala...';$wpis->save(); Obiekt Zend_Db_Table_Row

  33. Zend_Db_TableModyfikacja wierszy $dane = array('tytul' => 'Zmieniony tytuł','tresc' => 'Całkiem nowa treść' ); $model->update($dane, 'id=26'); Metoda update $wpis = $model->find(26)->current();$wpis->tytul = 'Zmieniony tytuł';$wpis->tresc = 'Całkiem nowa treść';$wpis->save(); Obiekt Zend_Db_Table_Row

  34. Zend_Db_TableKonstruowanie zapytań $select = $model->select();$select->where('data_dodania >= ?', '2010-03-01');$select->where('data_dodania < ?', '2010-04-01');$select->order('tytul');$res = $model->fetchAll($select); $select = $model->select()->where('data_dodania >= ?', '2010-03-01')->where('data_dodania < ?', '2010-04-01')->order('tytul');$res = $model->fetchAll($select);

  35. Zend_Db_TableKonstruowanie zapytań $select = $model->select(Zend_Db_Table::SELECT_WITH_FROM_PART);$select->where('data_dodania >= ?', '2010-03-01');$select->where('data_dodania < ?', '2010-04-01');$select->order('tytul'); $select->setIntegrityCheck(false);$select->join('uzytkownicy', 'wpisy.autor=uzytkownicy.id'); $res = $model->fetchAll($select);

  36. Automatyczne ładowanie klas • Zend_Loader automatycznie ładuje definicje klas, o ile spełniają warunki określone w konfiguracji • Klasa:Application_Model_Wpisy • Plik:/application/models/Wpisy.php • Klasa: Application_Form_Wpis • Plik:/application/forms/Wpis.php

  37. Budujemy bloga

  38. Nowy projekt – Nasz Blog zfcreateprojectNaszBlog cdNaszBlog

  39. Włączenie Zend_Layout zfenablelayout

  40. Efekt włączenia Zend_Layout • Domyślny szablon layout.phtml <?phpecho$this->layout()->content; ?>

  41. Nasz Blog – szablon layout.phtml <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>ZFBlog</title><metahttp-equiv="Content-Type" content="text/html; charset=UTF-8"><linkrel="stylesheet"type="text/css" href="/main.css"></head><body><divid="page"><divid="menu"><ahref="/">Strona główna</a> |<ahref="">Dodaj wpis</a> |<ahref="">Zaloguj</a></div><divid="content"><?phpecho$this->layout()->content; ?></div></div></body></html>

  42. Nasz Blog – widok index.phtml <h1>Witamy w Naszym Blogu</h1>

  43. Model Dostęp do bazy danych

  44. Schemat bazy danych

  45. Konfiguracja bazy danychapplication.ini [production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" appnamespace = "Application" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.params.displayExceptions = 0 resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" resources.db.adapter = "pdo_mysql" resources.db.params.host = "localhost" resources.db.params.username = "zf_blog" resources.db.params.password = "zf_blog" resources.db.params.dbname = "zf_blog" resources.db.params.charset = utf8 [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 resources.frontController.params.displayExceptions = 1 resources.db.adapter = "pdo_mysql" resources.db.params.host = "localhost" resources.db.params.username = "zf_blog" resources.db.params.password = "zf_blog" resources.db.params.dbname = "zf_blog" resources.db.params.charset = utf8

  46. Automatyczne ładowanie klas • application.ini • Klasy z przedrostkiem Blog_ będą automatycznie ładowane (bez require) [production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" appnamespace = "Blog" ...

  47. Zend_Db_Table • Tworzymy model dla tabeli wpisy • Klasa Blog_Model_Wpisy dziedzicząca z Zend_Db_Table_Abstract <?phpclassBlog_Model_WpisyextendsZend_Db_Table_Abstract{protected$_name = 'wpisy';}

  48. Pobieranie danych z modelu • Pobranie wszystkich wpisów: • fetchAll() • fetchAll($where, $order) • Pobranie wpisu o konkretnym ID: • find($id) $mdl = newBlog_Model_Wpisy();$wpisy = $mdl->fetchAll(); $mdl = newBlog_Model_Wpisy();$wpis = $mdl->find($id)->current();

  49. Pobieranie danych z modeluWyświetlenie wszystkich wpisów na stronie głównej • Pobranie wszystkich wpisów i przekazanie do widoku:(application/controllers/IndexController.php) classIndexControllerextendsZend_Controller_Action {publicfunctionindexAction() {$mdl = newBlog_Model_Wpisy();$wpisy = $mdl->fetchAll();$this->view->wpisy=$wpisy; } }

More Related