Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] FrontController nie potrafi wywołać innych klas kontrolerów
Forum PHP.pl > Forum > Przedszkole
framework
Witam

Od dłuższej chwili męczę się z stworzeniem FrontControllera do aplikacji. Problem ten wynika najprawdopodobniej z przestrzeni nazw, ponieważ bez nich to działa, ale zależało by mi na autoloadingu, dlatego też chciałem je dodać wzorując się na tym co tutaj zostało stworzone http://www.sitepoint.com/front-controller-pattern-1/

Na stronie cały czas otrzymuje bład:

Cytat
Fatal error: Class 'IndexController' not found in ...\Library\Controller\FrontController.php on line 76


Oczywiście błąd rozumiem, jednakże nie potrafię dać sobie rady, żeby się on nie pojawiał. Dlatego zwracam się do was o pomoc:
To tyle teorii, mój kod
index.php
  1. require_once 'Library/Controller/autoloader.php';
  2.  
  3. use Library\Controller\Autoloader,
  4. Library\Controller\FrontController;
  5.  
  6. $autoloader = new Autoloader('Library\Controller','.');
  7. $autoloader->register();
  8. $frontController = new FrontController();
  9. $frontController->run();


FrontController.php

  1. namespace Library\Controller;
  2.  
  3. class FrontController implements FrontControllerInterface
  4. {
  5. const DEFAULT_CONTROLLER = "IndexController";
  6. const DEFAULT_ACTION = "index";
  7.  
  8. protected $controller = self::DEFAULT_CONTROLLER;
  9. protected $action = self::DEFAULT_ACTION;
  10. protected $params = array();
  11. protected $basePath = "cgi/fc";
  12.  
  13. public function __construct(array $options = array()) {
  14. if (empty($options)) {
  15. $this->parseUri();
  16. }
  17. else {
  18. if (isset($options["controller"])) {
  19. $this->setController($options["controller"]);
  20. }
  21. if (isset($options["action"])) {
  22. $this->setAction($options["action"]);
  23. }
  24. if (isset($options["params"])) {
  25. $this->setParams($options["params"]);
  26. }
  27. }
  28. }
  29.  
  30. protected function parseUri() {
  31. $path = trim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), "/");
  32. $path = preg_replace('/[^a-zA-Z0-9]\//', "", $path);
  33. if (strpos($path, $this->basePath) === 0) {
  34. $path = substr($path, strlen($this->basePath));
  35. }
  36. @list($controller, $action, $params) = explode("/", $path, 3);
  37. if (isset($controller) && !empty($controller)) {
  38. $this->setController($controller);
  39. }
  40. if (isset($action) && !empty($action)) {
  41. $this->setAction($action);
  42. }
  43. if (isset($params) && !empty($params)) {
  44. $this->setParams(explode("/", $params));
  45. }
  46. }
  47.  
  48. public function setController($controller) {
  49. $controller = ucfirst(strtolower($controller)) . "Controller";
  50. if (!class_exists($controller)) {
  51. throw new \InvalidArgumentException(
  52. "The action controller '$controller' has not been defined.");
  53. }
  54. $this->controller = $controller;
  55. return $this;
  56. }
  57.  
  58. public function setAction($action) {
  59. $reflector = new \ReflectionClass($this->controller);
  60. if (!$reflector->hasMethod($action)) {
  61. throw new \InvalidArgumentException(
  62. "The controller action '$action' has been not defined.");
  63. }
  64. $this->action = $action;
  65. return $this;
  66. }
  67.  
  68. public function setParams(array $params) {
  69. $this->params = $params;
  70. return $this;
  71. }
  72.  
  73. public function run() {
  74. call_user_func_array(array(new $this->controller, $this->action), $this->params); // to wywołanie powoduje błąd, a dokładnie new $this->controller
  75. }
  76. }


IndexController.php
  1. namespace Library\Controller;
  2.  
  3. class IndexController {
  4. public function index(){
  5. echo 'a';
  6. }
  7. }


Autoloader.php
  1. namespace Library\Controller;
  2.  
  3. class Autoloader
  4. {
  5. private $fileExtension = '.php';
  6. private $namespace;
  7. private $includePath;
  8. private $namespaceSeparator = '\\';
  9.  
  10. public function __construct($namespace = null, $includePath = null)
  11. {
  12. $this->namespace = $namespace;
  13. $this->includePath = $includePath;
  14. }
  15.  
  16. public function setNamespaceSeparator($sep)
  17. {
  18. $this->namespaceSeparator = $sep;
  19. }
  20.  
  21. public function getNamespaceSeparator()
  22. {
  23. return $this->namespaceSeparator;
  24. }
  25.  
  26. public function setIncludePath($includePath)
  27. {
  28. $this->includePath = $includePath;
  29. }
  30.  
  31. public function getIncludePath()
  32. {
  33. return $this->includePath;
  34. }
  35.  
  36. public function setFileExtension($fileExtension)
  37. {
  38. $this->fileExtension = $fileExtension;
  39. }
  40.  
  41. public function getFileExtension()
  42. {
  43. return $this->fileExtension;
  44. }
  45.  
  46. public function register()
  47. {
  48. spl_autoload_register(array($this, 'loadClass'));
  49. }
  50.  
  51.  
  52. public function unregister()
  53. {
  54. spl_autoload_unregister(array($this, 'loadClass'));
  55. }
  56.  
  57. public function loadClass($className)
  58. {
  59. if (null === $this->namespace || $this->namespace.$this->namespaceSeparator === substr($className, 0, strlen($this->namespace.$this->namespaceSeparator))) {
  60. $fileName = null;
  61. $namespace = null;
  62. if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) {
  63. $namespace = substr($className, 0, $lastNsPos);
  64. $className = substr($className, $lastNsPos + 1);
  65. $fileName = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  66. }
  67. $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
  68. require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . $fileName;
  69. }
  70. }
  71. }


No i struktura katalogów to:
Kod
[Library] ->
        [Controller] ->
              //tu wszytkie kontrolery i autoload
index.php
Pyton_000
Kod
const DEFAULT_CONTROLLER = "IndexController";

Brakuje Namespace
Kod
const DEFAULT_CONTROLLER = "\Library\Controller\IndexController";
framework
Dzięki wielkie faktycznie pomogło, robiłem tak wcześniej to nie działało haha.gif to pewnie wtedy gdzie indziej był błąd wink.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.