早上好,

在过去的几周里,我一直在使用 Silex 开发一个应用程序,昨晚我要么对我的代码进行了更改,要么在更新 composer 的过程中更新了一些东西,但它不起作用。

我正在使用 \\’Igorw\\\\\\\\ConfigServiceProvider\\’ 来加载链接到我配置的控制器的路由。但是当我访问网页时,我收到错误消息:

1 InvalidArgumentException : Unable to find controller “controllers.admin:index” .

我的文件如下

composer.json

1
2
3
4
5
6
7
8
9
10
11
12
13
{
    “require” : {
        “silex/silex” : “1.2.*@dev” ,
        “igorw/config-service-provider” : “1.2.*@dev” ,
        “symfony/yaml” : “2.5.*@dev”
    } ,

    “autoload” : {
        “psr-4” : {
            “Turtle\\\\Controllers\”:”src /turtle /controllers
        }
    }
}

config/routes.yml

1
2
3
4
5
config .routes :
  admin :
    pattern : /admin
    defaults : { _controller : ‘controllers.admin:index’ }
    method : GET

web/index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

require_once __DIR__ . ‘/../vendor/autoload.php’ ;

use \\Igorw\\Silex\\ConfigServiceProvider ;
use \\Turtle\\Controllers\\AdminController ;

$app = new Silex\\Application ;

$app [ “debug” ] = true ;

// load the routes
$app -> register ( new ConfigServiceProvider (__DIR__ . “/../config/routes.yml” ) ) ;
foreach ( $app [ “config.routes” ] as $name => $route ) {
    $app -> match ( $route [ “pattern” ] , $route [ “defaults” ] [ “_controller” ] ) -> bind ( $name ) -> method ( isset ( $route [ “method” ] ) ? $route [ “method” ] : “GET” ) ;
}

// register the classes
$app [ “controllers.admin” ] = $app -> share ( function ( $app ) {
    return new AdminController ( $app ) ;
} ) ;

$app -> run ( ) ;

src/turtle/controllers/AdminController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

namespace Turtle\\Controllers ;

use Silex\\Application ;
use Symfony\\Component\\HttpFoundation\
equest ;

class AdminController {

    protected $app ;

    public function __construct (Application $app ) {
        $this -> app = $app ;
    }

    public function index (Request $request ) {
        return “Hello World!” ;
    }

}

我检查了 $app 变量,它包含一个实例化的 AdminController 类,但由于某种原因,系统正在正确地选择控制器。我真的不明白发生了什么,只能把它归结为一个模糊的错误或更新。

谁能解释一下这个问题?

谢谢,罗素


我在 Silex GitHub 问题网站 https://github.com/silexphp/Silex/issues/919 上交叉发布了此问题,问题已被指出。向戴夫·马歇尔致敬。

web/index.php 文件没有注册 Silex ServerControllerServiceProvider。在系统中添加它后,现在可以工作了。更新后的文件现在看起来像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

require_once __DIR__ . ‘/../vendor/autoload.php’ ;

use \\Igorw\\Silex\\ConfigServiceProvider ;
use \\Turtle\\Controllers\\AdminController ;

$app = new Silex\\Application ;

$app [ “debug” ] = true ;

$app -> register ( new Silex\\Provider\\ServiceControllerServiceProvider ( ) ) ;

// load the routes
$app -> register ( new ConfigServiceProvider (__DIR__ . “/../config/routes.yml” ) ) ;
foreach ( $app [ “config.routes” ] as $name => $route ) {
    $app -> match ( $route [ “pattern” ] , $route [ “defaults” ] [ “_controller” ] ) -> bind ( $name ) -> method ( isset ( $route [ “method” ] ) ? $route [ “method” ] : “GET” ) ;
}

// register the classes
$app [ “controllers.admin” ] = $app -> share ( function ( $app ) {
    return new AdminController ( $app ) ;
} ) ;

$app -> run ( ) ;

我一定是在重新组织文件时无意中删除了该行。



相关讨论

  • 同样的事情发生在我身上,添加注册行解决了它。


声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。