Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [OOP][MVC][Routing] Jak wywołać z routingu kontroler i wyświetlić akcje
Forum PHP.pl > Forum > PHP
in5ane
Hej, piszę dla nauki framework. Utknąłem w środku routingu, odbieram kontroler, akcje i parametry z adresu i teraz nie wiem, jak mam wywołać dany kontroler. Może ktoś mnie nakieruje? Oto mój router:

  1. <?php
  2.  
  3. namespace System\Core;
  4.  
  5. use System\Core\Uri;
  6.  
  7. class Router
  8. {
  9.  
  10. public $uri;
  11. public $controller;
  12. public $method;
  13. public $params = array();
  14.  
  15. public function __construct()
  16. {
  17. $returnPath = new Uri;
  18. $this->uri = $returnPath->returnPath();
  19. $this->parseRoutes();
  20. }
  21.  
  22. private function parseRoutes()
  23. {
  24. if (empty($this->uri)) {
  25. echo 'default controller';
  26. }
  27. else
  28. {
  29. $segments = array(explode('/', $this->uri));
  30. $this->setRoutes($segments[0]);
  31. }
  32. }
  33.  
  34. private function setRoutes($segments)
  35. {
  36. $this->controller = $segments[0];
  37. if (!empty($segments[1]))
  38. {
  39. $this->method = $segments[1];
  40. for ($i = 2; $i < count($segments); $i++)
  41. {
  42. $this->params[$i] = $segments[$i];
  43. }
  44. }
  45. else
  46. {
  47. $this->method = 'index';
  48. }
  49.  
  50. /* TEST: */
  51. echo 'Kontroler: '. $this->controller;
  52. echo '<br />Akcja: '. $this->method;
  53. for ($i = 2; $i < count($segments); $i++)
  54. {
  55. $j = $i - 1;
  56. echo '<br />Parametr '. $j .': '. $this->params[$i];
  57. }
  58. }
  59.  
  60. }
mstraczkowski
Przyda ci się funkcja call_user_func_array(array($obiektController, $stringAkcja), $parametryAkcji)
nospor
Problem z OOP nie ma zadnego zwiazku - przenosze
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.