Autoloading controllers

I am having an issue with autoloading a custom controller. I keep getting a 500 fatal error and I am not 100% sure why this is.

I have a tree built like
`

    • public
    • src
    • | - - DashController
      `

My composer.json looks like
"autoload": { "psr-4": { "App\\": "/src" } },

MyController is looks like this

`
namespace App;

class DashAction
{
protected $ci;

public function __construct( ContainerInterface $ci )
{
    $this->ci = $ci;
}


public function Data( $request, $response, $args )
{
    $this->ci->get( 'view' );
}

}`

dependency and route
$container[App\DashAction::class] = function ( $c ) { return new App\DashAction( $c ); };

$app->get('/dashboard', App\DashAction::class . ":Data" );

Not seeing what I am missing here for the class to autoload properly. Maybe someone here might have a better idea.

For the assignment of dependencies into the container, I use this syntax:

$container['App\Controller\UserController'] = function ($c) {
    return new App\Controller\UserController($c);
};

But I think your problem in the way you define the function the controller should invoke. Try this:

$app->get('/dashboard', 'App\DashAction:Data' )->setName ('blah'); 

Hope this helps.

Did you call composer dumpautoload to update the loader after updating the composer.json?

Also I see you mixed up DashController and DashAction.

The PHP ::class constant you use to reference to a class is fine :wink:

Your action (the Data() method) is not returning anything. Maybe try to return the $response;

If the above does not help you can you specify what the error is? Just 500 doesn’t really give a clue where to look :wink:
Can you either check the webserver log file or turn on displayErrorDetails

I used another way to register my controllers, following the middleware structure:

  • Route
$app = new Slim\App();

$app->get('/', App\Controller\Home::class);

For the middleware controller:

namespace App\Controller;

use Interop\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class Home
{
    private $container;

    public function __construct(ContainerInterface $conteiner)
    {
         $this->container = $container;
    }

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args)
    {
        return $response->write('Hello World!');
    
}

This is a simple example, for my work I used a MiddlewareControllerInterface class and MiddlewareControllerAbstract class for make the constructor only one time, but it’s a good start for you I think.