1

我在让类自动加载在我正在组装的 Silex 骨架中工作时遇到问题。我在另一个项目中有这个工作,但我不知道我在这里做错了什么。我的目录如下所示:

root
 -src
    -Controller
       -HelloController.php
    -app.php
 -vendor
 -web
   -index.php
 -composer.json

这是我的 index.php

<?php
$app = require __DIR__.'/../src/app.php';
$app->run();

应用程序.php

<?php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
$app->get("/hello/{name}", 'App\Controller\HelloController::hello');
return $app;

你好控制器.php

<?php

namespace App\Controller;

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

class HelloController
{
    public function hello($name)
    {
        return new Response('<html><head></head><body><h1>Hello, '.$name.'</h1></body></html>');
    }
}

和 composer.json

{
    "require": {
        "silex/silex": "^1.3"
    },
    "autoload": {
      "psr-4": {
        "App\\": "/src"
      }
    }
}

每当我尝试在浏览器中打开 index.php/hello/world 时,都会收到此错误:

InvalidArgumentException in ControllerResolver.php line 153: 
Class "App\Controller\HelloController" does not exist
4

2 回答 2

4

psr-4的不工作。在前面加一个点'/src'

"App\\": "./src"

或将斜线移到末尾

"App\\": "src/"

或者只是删除斜线。路径必须是相对的。

于 2015-08-09T18:14:57.153 回答
-1

在 composer.json 更改

"App\\": "/src"

"App\\": "src"

您可以在 GitHub 上找到 带有控制器的 SilexSkeleton 作为 php 类 示例:https ://github.com/jaresz/SilexSkeleton

于 2016-07-16T22:04:23.487 回答