Recency
Recency

Reputation: 27

Slim Application Error due not a class or valid container entry

I am trying to use the slim framework to build a website, however I get the following error: "Cannot call index on Store\HomeController because it is not a class nor a valid container entry." I have thoroughly checked for spelling and punctuation errors and found none. The error go's to line 98 of a "callableResover.php file of the following code:

throw new NotCallableException(sprintf(
    'Cannot call %s on %s because it is not a class nor a valid container entry',
    $callable[1],
    $callable[0]
));

This is the HomeController.php file the I created as the following: namespace Store\Controllers;

class HomeController{
  public function index(){
    echo('Index');

  }//end function index

}//end class

And this the route.php file of the code following:

$app->get('/', ['Store\HomeController', 'index'])->setName('home');

Upvotes: 0

Views: 1683

Answers (2)

WhipsterCZ
WhipsterCZ

Reputation: 657

I found a problem.. The class is probably registred in /vendor/composer/autoload_classmap.php

so you have to regenerate it with command composer dump-autoload

and it should work...

Upvotes: 1

micster
micster

Reputation: 758

Assuming your HomeController file is loaded before using it in your route.php file, try to:

1. Add a \ to your controller's namespace in the route definition

\Store\HomeController

2. Change your route to this one instead

$app->get('/', \Store\HomeController::class . ':index');

Check the Slim documentation to learn more about container resolution

Upvotes: 2

Related Questions