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
require_once 'Library/Controller/autoloader.php'; use Library\Controller\Autoloader, Library\Controller\FrontController; $autoloader = new Autoloader('Library\Controller','.'); $autoloader->register(); $frontController = new FrontController(); $frontController->run();
FrontController.php
namespace Library\Controller; class FrontController implements FrontControllerInterface { const DEFAULT_CONTROLLER = "IndexController"; const DEFAULT_ACTION = "index"; protected $controller = self::DEFAULT_CONTROLLER; protected $action = self::DEFAULT_ACTION; protected $basePath = "cgi/fc"; $this->parseUri(); } else { $this->setController($options["controller"]); } $this->setAction($options["action"]); } $this->setParams($options["params"]); } } } protected function parseUri() { } $this->setController($controller); } $this->setAction($action); } } } public function setController($controller) { if (!class_exists($controller)) { throw new \InvalidArgumentException( "The action controller '$controller' has not been defined."); } $this->controller = $controller; return $this; } public function setAction($action) { $reflector = new \ReflectionClass($this->controller); if (!$reflector->hasMethod($action)) { throw new \InvalidArgumentException( "The controller action '$action' has been not defined."); } $this->action = $action; return $this; } $this->params = $params; return $this; } public function run() { call_user_func_array(array(new $this->controller, $this->action), $this->params); // to wywołanie powoduje błąd, a dokładnie new $this->controller } }
IndexController.php
namespace Library\Controller; class IndexController { public function index(){ } }
Autoloader.php
namespace Library\Controller; class Autoloader { private $fileExtension = '.php'; private $namespace; private $includePath; private $namespaceSeparator = '\\'; public function __construct($namespace = null, $includePath = null) { $this->namespace = $namespace; $this->includePath = $includePath; } public function setNamespaceSeparator($sep) { $this->namespaceSeparator = $sep; } public function getNamespaceSeparator() { return $this->namespaceSeparator; } public function setIncludePath($includePath) { $this->includePath = $includePath; } public function getIncludePath() { return $this->includePath; } public function setFileExtension($fileExtension) { $this->fileExtension = $fileExtension; } public function getFileExtension() { return $this->fileExtension; } public function register() { } public function unregister() { } public function loadClass($className) { $fileName = null; $namespace = null; if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) { $fileName = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . $fileName; } } }
No i struktura katalogów to:
Kod
[Library] ->
[Controller] ->
//tu wszytkie kontrolery i autoload
index.php
[Controller] ->
//tu wszytkie kontrolery i autoload
index.php