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.phprequire_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.phpnamespace 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
$params = array(); protected $basePath = "cgi/fc";
public function __construct
(array $options = array()) { $this->parseUri();
}
else {
if (isset($options["controller"])) { $this->setController($options["controller"]);
}
if (isset($options["action"])) { $this->setAction($options["action"]);
}
if (isset($options["params"])) { $this->setParams($options["params"]);
}
}
}
protected function parseUri() {
$path = trim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH
), "/"); if (strpos($path, $this->basePath) === 0
) { }
@list
($controller, $action, $params) = explode("/", $path, 3
); $this->setController($controller);
}
$this->setAction($action);
}
$this->setParams(explode("/", $params)); }
}
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;
}
public function setParams
(array $params) { $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.phpnamespace Library\Controller;
class IndexController {
public function index(){
}
}
Autoloader.phpnamespace 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()
{
spl_autoload_register
(array($this, 'loadClass')); }
public function unregister()
{
spl_autoload_unregister
(array($this, 'loadClass')); }
public function loadClass($className)
{
if (null === $this->namespace || $this->namespace.$this->namespaceSeparator === substr($className, 0
, strlen($this->namespace.$this->namespaceSeparator))) { $fileName = null;
$namespace = null;
if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) {
$namespace = substr($className, 0
, $lastNsPos); $className = substr($className, $lastNsPos + 1
); $fileName = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR
, $namespace) . DIRECTORY_SEPARATOR
; }
$fileName .= str_replace('_', DIRECTORY_SEPARATOR
, $className) . $this->fileExtension; 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
Ten post edytował framework 15.01.2015, 23:12:26