在Controller

时间:2016-08-21 00:13:40

标签: php silex

执行api.php时:

  

致命错误:第9行的C:\ xampp \ htdocs \ lab \ src \ Api \ UserController.php中找不到接口'Silex \ ControllerProviderInterface'

作曲:

{
    "require": {
        "silex/silex": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "Api\\": "src/Api"
        }
    }
}

api.php:

<?php
require_once __DIR__.'/vendor/autoload.php';
$app = new Silex\Application();

$app['debug'] = true;

$app->mount('/', new Api\UserController());

$app->run();

src / Api / UserController.php:

<?php
namespace Api;

use Symfony\Component\HttpFoundation\Request;
use Silex\Application;
use Silex\ControllerProviderInterface;

class UserController implements ControllerProviderInterface {

  public function connect(Application $app) {
    $factory=$app['controllers_factory'];
    $factory->get('/','Api\UserController::home');
    return $factory;
  }

  public function home() {
    return 'Hello world';
  }

}

如何组织文件:

root folder
- composer.json
- api.php
-  src 
        - Api
              - UserController.php
- vendor (with silex files)

Silex由作曲家composer require silex/silex *安装在根文件夹中。

1 个答案:

答案 0 :(得分:6)

Silex 2.0引入了一些BC breacks。其中之一是创建Silex\Api namespace where the ControllerProviderInterface resides now

所以你必须重构你的UserController:

<?php
namespace Api;

use Symfony\Component\HttpFoundation\Request;
use Silex\Application;
use Silex\Api\ControllerProviderInterface;
//       ^^^^^

class UserController implements ControllerProviderInterface {
// ...