所以我确实尝试了没有苗条的命名空间,它没有任何问题,但是当苗条一直显示时:
500 server error
Composer.json:
{
"require":{
"slim/slim":"2.*",
"slim/extras":"2.*",
"twig/twig":"1.*",
"slim/pdo": "~1.6"
},
"autoload":{
"psr-4":{
"Ultimateboard\\": "src"
}
}
}
MainController.php
namespace Ultimateboard;
class MainController {
function __construct(){
}
public function index(){
echo 'hello main controller index';
}
}
的index.php
require_once 'vendor/autoload.php';
require_once 'app/loads/routes.php';
我怎么试着调用这些方法:
1 Routes.php
$app = new \Slim\Slim(
array(
'view'=> new \Slim\Extras\Views\Twig(),
'debug'=> true
)
);
$app->get('/',Ultimateboard\MainController);
$app->get('/testing',function(){
echo 'testing works';
});
$app->run();
或
Routes.php
$app = new \Slim\Slim(
array(
'view'=> new \Slim\Extras\Views\Twig(),
'debug'=> true
)
);
$app->get('/',function(){
use \Ultimateboard\MainController as MainController;
new MainController()->index();
});
$app->get('/testing',function(){
echo 'testing works';
});
$app->run();
目录结构:
也许我错过了某些东西或与某些东西发生冲突,确实在不同的网站上尝试了很多答案,但是为什么在这里张贴它就能解决问题。
答案 0 :(得分:1)
通过将名称空间更改为
来重新转发namespace \Ultimateboard\Controllers;
和路线:
$app->get('/',function() use ($app){
(new Ultimateboard\Controllers\Maincontroller())->index();
});