kwinkel
kwinkel

Reputation: 177

PHP PSR-4 - Class not found

I've added my namespace with PHP, but can't get it working.

What I am doing wrong with my setup? When I want to call my test-class:

https://hilfe.kbs-community.de/index.php?controller=TanoaLife&params=123

I get the error message:

Class 'KWinkel\Helpdesk\Controller\TanoaLife' not found

My setup:

index.php
<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');
ob_start('ob_gzhandler');

// autoloader 
$Autoloader = require __DIR__ . '/vendor/autoload.php';
$Autoloader->add('KWinkel\\Helpdesk\\', 'app/');

// controller
if ( isset($_GET["controller"]) ) {
    $Controller = $_GET["controller"];
    if ( file_exists("app/Controller/" . $Controller . ".class.php") ) {
        $Class = "KWinkel\Helpdesk\Controller\\" . $Controller;
        new $Class($_GET["params"]);
    } else {
        echo "invalid call #1";
    }
} else {
    echo "invalid call #2";
}

$SysContent = ob_get_contents();
ob_end_clean();
echo $SysContent;

?>


app/Controller/TanoaLife.class.php
<?php
namespace KWinkel\Helpdesk\Controller;

class TanoaLife extends AbstractController {
    //

    function __construct ($Params) {
        echo "params: " . $Params;
    }

}


?>

Upvotes: 1

Views: 635

Answers (1)

JustOnUnderMillions
JustOnUnderMillions

Reputation: 3795

Your classfile TanoaLife.class.php should look like this:

 namespace KWinkel\Helpdesk\Controller;

 class TanoaLife {

 }

and should be placed here:

app/Controller/TanoaLife.class.php

to work with the Autoloader.

UPDATE: I would prefer to name the classfile TanoaLife.php instead of TanoaLife.class.php or you have to setup the autoloader to include classes with suffix .class.php

$Autoloader->addPsr4('KWinkel\\Helpdesk\\', 'app');

https://github.com/composer/composer/blob/master/src/Composer/Autoload/ClassLoader.php

Upvotes: 1

Related Questions