简体   繁体   中英

Controller not found in slim 3

I am new to slim,so please bear with me. I have project structure like

\slim
  \public
    index.php
    
  \src
    \controllers
         \reports
        BillingReportController.php
       \routes
         router.php
       \config
         db.php

But whenever I call the controller via route it gives me below error

"PHP Fatal error: Class 'src\\controllers\\reports\\BillingReportController' not found in /var/www/html/apimod/public/index.php on line 13"

As for the line mentioned in error the snippet is as follows.

index.php

$container = $app->getContainer();
$container['BillingReportController'] = function($container){
    return new \src\controllers\reports\BillingReportController;
}; 

router.php

$app->group('/report', function() use ($app) {

  $app->get('/billing', 'BillingReportController:billing');
});

BillingReportController.php

namespace src\controllers\BillingReportController;

class BillingReportController{
    public function billing($request, $response){
        //my code goes here
    }
}

Can anyone please point out the error.

You must've to use autoloading in your composer. something like this.

"autoload": {
"psr-4": {
  "src\\": "src"
 }
}

then in your terminal enter this command

composer dump-autoload

it should fix your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM