Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Framework - problem z dostępem do właściwości statycznej
Forum PHP.pl > Forum > PHP > Object-oriented programming
blackneron
Mam oto dość prosty i typowy dla architektury MVC framework.
Problem jest w załadowaniu metody statycznej ładującej szablon (klasy dispatcher) - _loadTemplate()
Treść błędu: "Fatal error: Call to undefined method base::_loadTemplate() in ..."
Nie wiem, co jest tego powodem bo odwołanie do niej jest prawidłowe (tj. base::_loadTemplate($controller, $template, $viewvars, $uselayout)winksmiley.jpg, i ogólnie kontroler zostaje poprawnie załadowany, tylko widok nie.
Podejrzewałem że mam namieszane w httpd.conf, z obsługą mod_rewrite lub z php.ini ale chyba wszystko tam gra ...

httpd.conf
[APACHE] pobierz, plaintext
  1. <Directory />
  2. Options FollowSymLinks
  3. AllowOverride All
  4. Order deny,allow
  5. Deny from all
  6. Satisfy all
  7. </Directory>
[APACHE] pobierz, plaintext


.htaccess
[APACHE] pobierz, plaintext
  1. RewriteEngine on
  2. RewriteCond $1 !^(index\.php|images|robots\.txt)
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteRule ^(.*)$ index.php?$1
[APACHE] pobierz, plaintext



index.php
  1. <?php
  2. /*
  3. * index.php
  4. * Jest to plik rozruchowy
  5. */
  6. include ("core/ini.php");
  7. initializer::initialize();
  8. /* obiekt o nazwie loader - on ma wczytać obiekty
  9. * za pomocą wzorca Singletone (żeby zmniejszyć obciążenie systemu
  10. */
  11.  
  12. $router = loader::load("router");
  13. dispatcher::dispatch($router);
  14.  
  15.  
  16.  
  17. ?>


core/main/router.php
  1. <?php
  2. /**
  3. *
  4. * Zadaniem router'a jest odszukanie kontrolera, akcji i parametrów w żądaniu URL
  5. *
  6. */
  7.  
  8. class router
  9. {
  10. private $route;
  11. private $controller;
  12. private $action;
  13. private $params;
  14.  
  15. public function __construct()
  16. {
  17. if(file_exists("app/config/routes.php"))
  18. {
  19. require_once("app/config/routes.php");
  20. }
  21.  
  22. $path = array_keys($_GET);
  23. $config = loader::load("config");
  24.  
  25. if(!isset($path[0]))
  26. {
  27. $default_controller = $config->default_controller; // w configs.php jest odwołanie
  28. if(!empty($default_controller))
  29. $path[0] = $default_controller;
  30. else
  31. $path[0] = "index";
  32. }
  33.  
  34. $route = $path[0];
  35. $sanitzing_pattern = $config->allowed_url_chars; // w configs.php jest odwołanie
  36. $route = preg_replace($sanitzing_pattern, "", $route);
  37. $route = str_replace("^", "", $route);
  38. $this->route = $route;
  39. $routeParts = explode("/", $route);
  40. $this->controller = $routeParts[0];
  41. $this->action = isset($routeParts[1]) ? $routeParts[1]: "base";
  42. array_shift($routeParts);
  43. array_shift($routeParts);
  44. $this->params = $routeParts;
  45.  
  46. // Dopasowanie wzorca routingu zdefiniowanego przez użytkownika
  47. if(isset($routes))
  48. {
  49. foreach($routes as $_route)
  50. {
  51. $_pattern = "~{$_route[0]}~";
  52. $_destination = $_route[1];
  53.  
  54. if(preg_match($_pattern, $route))
  55. {
  56. $newrouteparts = explode("/", $_destination);
  57. $this->controller = $newrouteparts[0];
  58. $this->action = $newrouteparts[1];
  59. }
  60. }
  61. }
  62.  
  63. }
  64.  
  65. public function getAction()
  66. {
  67. if(empty($this->action)) $this->action = "main";
  68. return $this->action;
  69. }
  70.  
  71. public function getController()
  72. {
  73. return $this->controller;
  74. }
  75.  
  76. public function getParams()
  77. {
  78. return $this->params;
  79. }
  80.  
  81. }
  82.  
  83. ?>



core/main/dispatcher.php
  1. <?php
  2.  
  3. /**
  4. * Klasa dispatcher
  5. *
  6. * Głównym zadaniem tej klasy jest pobranie obiektu router'a jako parametru
  7. * a następnie odszukanie kontrolera, akcji i parametrów z router'a
  8. * Jeżeli plik kontrolera będzie dostępny to zostanie wczytany i nastąpi
  9. * inicjalizacja kotrolera. Po zakończeniu inicjalizacji zostanie wykonana akcja
  10. *
  11. */
  12.  
  13. class dispatcher
  14. {
  15. public static function dispatch($router)
  16. {
  17. global $app;
  18. $config = loader::load("config");
  19.  
  20. // configs.php - odwołanie
  21. if($config->global_profile) $start = microtime(true);
  22. // poniżej ważna część kodu
  23. $controller = $router->getController();
  24. $action = $router->getAction();
  25. $params = $router->getParams();
  26.  
  27. if(count($params)>1)
  28. {
  29. if("unittest" == $params[count($params)-1] ||
  30. '1' == $_POST['unittest']) unittest::setUp();
  31. }
  32. $controllerfile = "app/controllers/{$controller}.php";
  33.  
  34. if(file_exists($controllerfile))
  35. {
  36. require_once($controllerfile);
  37. $app = new $controller;
  38. $app->use_layout = true;
  39. $app->$action;
  40. unittest::tearDown();
  41.  
  42.  
  43. /**************************
  44. * Zarządzanie widokiem *
  45. **************************/
  46. $view = loader::load("view");
  47. $viewvars = $view->getVars($app);
  48. $uselayout = $config->use_layout;
  49.  
  50. if(!$app->use_layout) $uselayout=false;
  51. $template = $view->getTemplate($action);
  52.  
  53. base::_loadTemplate($controller, $template, $viewvars, $uselayout);
  54.  
  55. if(isset($start))
  56. echo "<p>Całkowity czas rozdzielenia:"
  57. .(microtime(true)-$start)." sekund .</p>";
  58. $output = ob_get_clean();
  59. echo $output;
  60. }
  61. else
  62. throw new Exception("Kontroller nie został znaleziony");
  63.  
  64.  
  65. }
  66.  
  67. /**
  68.   * TEJ METODY STATYCZNEJ NIE MOŻE ZNALEŹĆ[/b]
  69.   *
  70.   */
  71. public static function _loadTemplate($controller, $template, $vars, $uselayout=false)
  72. {
  73. extract($vars);
  74.  
  75. if($uselayout)
  76. $templatefile = "app/views/{$controller}/{$template}.php";
  77.  
  78. if(file_exists($templatefile))
  79. {
  80. include_once($templatefile);
  81. }
  82. else
  83. {
  84. throw new Exception("Widok '{$template}.php' nie został znaleziony w katalogu views/{$controller}.");
  85. }
  86. if($uselayout)
  87. {
  88. $layoutdata = ob_get_clean();
  89. $layoutfilelocal = "app/views/{$controller}.php";
  90. $layoutfileglobal = "app/views/layouts/{$controller}.php";
  91.  
  92. if(file_exists($layoutfilelocal))
  93. include_once($layoutfilelocal);
  94. else
  95. include_once($layoutfileglobal);
  96. }
  97.  
  98. }
  99.  
  100.  
  101.  
  102. }
  103.  
  104. ?>
  105.  
destroyerr
Co ma niezdefiniowana metoda do ustawień apache? Nie pokazałeś nam klasy base, albo się pomyliłeś i zamiast tego miało być self lub dispatcher.
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.