F
F
feycot2019-05-11 10:50:37
PHP
feycot, 2019-05-11 10:50:37

Why is Slim + PHP-DI throwing Fatal error: Cannot declare class when requesting a controller?

Good afternoon, I created a project on SLIM + PHP-DI, connected `php-di/slim-bridge` and inherited the application

class App extends \DI\Bridge\Slim\App
{
    protected function configureContainer($builder)
    {
        $builder->addDefinitions(dirname(__DIR__) . '/app/config.php');
    }
}

In `index.php` created an Application object and a test path
$app->get('/game/{id}', ['fey\Api\v1\Controllers\GameController', 'show']);

The problem is that for some reason the controller class is declared several times and gives an error. The controller itself is connected as standard via composer PSR-4 autoload
Fatal error: Cannot declare class Api\v1\Controllers\GameController, because the name is already in use in

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antony Tkachenko, 2019-05-11
@feycot

The second argument to get expects a callable as I understand it.
And in your case, it should most likely be something like

use Api\v1\Contollers\GameController;

$app->get('/game/{id}', [new GameController(), 'show']);
/* Или
$app->get('/game/{id}', [GameController::class, 'show']);
*/

Callable pseudotype

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question