Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Prosta aplikacja oparta na MVC
Forum PHP.pl > Forum > PHP
sztosz
Pamiętam ze ktoś kiedys dał linka do aplikacji typu "Hello World" opartej na MVC. Bedę wdzięczny jeżeli ktoś mi pomoże znaleźć to, albo poda jakiegoś linka do kompletnej, niewielkiej (chodzi mi o zobaczenie MVC w php w praktyce). Szukam od rana tego, ale znajduję tylkosamą teorię, a tą jako tako znam, i potrzeba mi tylko konkretnego rozwiązania, aby sprawdzić swoje założenia i domysły.

Jeżeli ktoś będzie łaskaw podać mi linka do tematu z tym linkiem do "Hello World" opartego na MVC, to można od razu ten temat zamknąć/w kosz/unicestwić, oczywiście jeżeli tylko ten link do mnie dojdzie winksmiley.jpg
mike
  1. <?php
  2. /********************************************************************
  3. Model-View-Controller implementation according to POSA
  4. (Pattern-Oriented Software Architecture
  5.   http://www.hillside.net/patterns/books/Siemens/book.html)
  6. ********************************************************************/
  7.  
  8. class HelloWorldController {
  9. private $model;
  10. function __construct($model) {
  11. $this->model = $model;
  12. }
  13.  
  14. function handleEvent($args) {
  15. $this->model->setStrategy($args[2]);
  16. $this->model->addText($args[1]);
  17. }
  18. }
  19.  
  20.  
  21. class HelloWorldModel {
  22. private $text;
  23. private $observers = array();
  24. private $strategy;
  25.  
  26. function attach($observer) {
  27. $this->observers[] = $observer;
  28. }
  29.  
  30. function getData() {
  31. $facade = new HelloWorldFacade($this->strategy);
  32. return $facade->getHelloWorld().$this->text."n";
  33. }
  34.  
  35. function addText($text='') {
  36. $this->text = $text;
  37. $this->notify();
  38. }
  39.  
  40. function setStrategy($strategy) {
  41. $this->strategy = $strategy;
  42. }
  43.  
  44. function notify() {
  45. foreach ($this->observers as $observer) {
  46. $observer->update();
  47. }
  48. }
  49. }
  50.  
  51. class HelloWorldView {
  52. private $model;
  53.  
  54. function initialize($model) {
  55. $this->model = $model;
  56. $model->attach($this);
  57. return $this->makeController();
  58. }
  59.  
  60. function makeController() {
  61. return new HelloWorldController($this->model);
  62. }
  63.  
  64. function update() {
  65. $this->display();
  66. }
  67.  
  68. function display() {
  69. echo $this->model->getData();
  70. }
  71. }
  72.  
  73.  
  74. /*********************************************************************
  75. "Business logic"
  76. ********************************************************************/
  77.  
  78. class HelloWorld {
  79.  function execute() {
  80.  return "Hello world";
  81.  }
  82. }
  83.  
  84. class HelloWorldDecorator {
  85.  private $helloworld;
  86.  function __construct($helloworld) {
  87.  $this->helloworld = $helloworld;
  88.  }
  89.  
  90.  function execute() {
  91.  return $this->helloworld->execute();
  92.  }
  93. }
  94.  
  95. abstract class HelloWorldEmphasisStrategy {
  96. abstract function emphasize($string);
  97. }
  98.  
  99. class HelloWorldBangEmphasisStrategy extends HelloWorldEmphasisStrategy {
  100. function emphasize($string) {
  101.  return $string."!";
  102. }
  103. }
  104.  
  105. class HelloWorldRepetitionEmphasisStrategy extends HelloWorldEmphasisStrategy {
  106. function emphasize($string) {
  107.  return $string." and ".$string." again";
  108. }
  109. }
  110.  
  111. class HelloWorldEmphasizer extends HelloWorldDecorator {
  112.  private $strategy;
  113.  function HelloWorldEmphasizer($helloworld,$strategy) {
  114.  $this->strategy = $strategy;
  115.  parent::__construct($helloworld);
  116.  }
  117.  
  118.  function execute() {
  119.  $string = parent::execute();
  120.  return $this->strategy->emphasize($string);
  121.  }
  122. }
  123.  
  124. class HelloWorldStrategyFactory {
  125. static function make($type) {
  126. if ($type == 'repetition') return self::makeRepetitionStrategy();
  127. return self::makeBangStrategy();
  128. }
  129.  
  130. static function makeBangStrategy() {
  131. return new HelloWorldBangEmphasisStrategy;
  132. }
  133. static function makeRepetitionStrategy() {
  134. return new HelloWorldRepetitionEmphasisStrategy;
  135. }
  136. }
  137.  
  138. class HelloWorldFormatter extends HelloWorldDecorator {
  139.  function execute() {
  140.  $string = parent::execute();
  141.  return $string."n";
  142.  }
  143. }
  144.  
  145. class HelloWorldFacade {
  146. private $strategy;
  147. function __construct($strategyType) {
  148. $this->strategy = HelloWorldStrategyFactory::make($strategyType);
  149. }
  150.  
  151. function getHelloWorld() {
  152. $formatter = new HelloWorldFormatter(
  153. new HelloWorldEmphasizer(
  154. new HelloWorld,$this->strategy));
  155. return $formatter->execute();
  156. }
  157. }
  158.  
  159. $model = new HelloWorldModel;
  160. $view = new HelloWorldView;
  161. $controller = $view->initialize($model);
  162. $controller->handleEvent($_SERVER['argv']);
  163.  
  164. ?>


LOL,

sorki ale nie mogłem się powstrzymać biggrin.gif
sztosz
Dzięki smile.gif Temat w kosz, chyba że się komuś przyda winksmiley.jpg Troszkę to mi pomogło i troszkę nmiesało winksmiley.jpg
Sh4dow
blink.gif questionmark.gif

Jesli to napisales dla prostego przykladu to zaczynam sie obawiac o twoje zdrowie Rkingsmiley.png
mozna bylo by to spokojnie skrocic o polowe albo i wiecej. Wczytac sie w kod nie jest prosto a niektore metody wygladaja na poprostu zbedne.

Pozdrawiam Rkingsmiley.png
mynio
ten kod pochodzi ze strony phppatterns, więc nie był pisany po by odpowiedzieć na tego posta

pzdr
limak
http://www.phpsolmag.org/pl/modules/wmpdow...hp?cid=1&lid=13

^ tu masz za darmoche art z php solutions który opisuje tworzenie proces tworzenia aplikacji opartej o MVC (tylko trzeba sie wczesniej zarejestrowac jesli nie masz tam konta)
ActivePlayer
Cytat(Sh4dow @ 2005-08-18 14:56:51)
blink.gif questionmark.gif

Jesli to napisales dla prostego przykladu to zaczynam sie obawiac o twoje zdrowie Rkingsmiley.png
mozna bylo by to spokojnie skrocic o polowe albo i wiecej. Wczytac sie w kod nie jest prosto a niektore metody wygladaja na poprostu zbedne.

Pozdrawiam Rkingsmiley.png

Conajmniej o połowe ?tongue.gif

to ja Ci pokażę biggrin.gif

  1. <?php
  2.  
  3. echo 'Hello World !';
  4.  
  5. ?>
limak
Cytat(ActivePlayer @ 2005-08-18 21:42:14)
Cytat(Sh4dow @ 2005-08-18 14:56:51)
blink.gif questionmark.gif

Jesli to napisales dla prostego przykladu to zaczynam sie obawiac o twoje zdrowie  Rkingsmiley.png
mozna bylo by to spokojnie skrocic o polowe albo i wiecej. Wczytac sie w kod nie jest prosto a niektore metody wygladaja na poprostu zbedne.

Pozdrawiam  Rkingsmiley.png

Conajmniej o połowe ?tongue.gif

to ja Ci pokażę biggrin.gif

  1. <?php
  2.  
  3. echo 'Hello World !';
  4.  
  5. ?>

to mozna jeszcze krócej tongue.gif:P:P

  1. <?echo 'hello world!';?>


cool.gif
ActivePlayer
  1. <?='hello world!'?>
limak
aaa.. tak... racja... zapomialem smile.gif teraz mi sie przypomniało ze przeciez <?=SID?> sie czesto stosuje biggrin.gif
mike
No co wy, chłopaki.
Je tego nie napisałem. Kod ten pochodzi z phppatterns, a wkleiłem go trochę dla jaj tongue.gif
limak
@mike_mech: taa, wiemy wiemy, przeciez napisales "LOL, nie moglem sie powstrzymac" biggrin.gif
Ociu
Fajnie się gawędzi nie prawda ? IMHO temat do zamknięcia.
tiraeth
No to klozet smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.