虽然文件存在,但找不到类'Proxy \ Factory'PHP错误

时间:2015-09-22 22:16:10

标签: php symfony

我下载了这个lib,将其解压缩并上传到我的php网站空间:https://github.com/jenssegers/php-proxy

另外,我添加了一个新的index.php文件,其中包含了lib存档中包含的示例内容:

<?php

use Proxy\Factory;
use Proxy\Response\Filter\RemoveEncodingFilter;
use Symfony\Component\HttpFoundation\Request;

require 'vendor/autoload.php';

// Create the proxy factory.
$proxy = Factory::create();

// Add a response filter that removes the encoding headers.
$proxy->addResponseFilter(new RemoveEncodingFilter());

// Create a Symfony request based on the current browser request.
$request = Request::createFromGlobals();

// Forward the request and get the response.
$response = $proxy->forward($request)->to('http://example.com');

// Output response to the browser.
$response->send();

?>

文件结构如下(折叠):

Folder hierachy collcapsed

展开:

Expanded folder hierarchy

但是当我运行脚本时,我收到了这个错误:

  

致命错误:第10行的index.php中找不到“Proxy \ Factory”类

第12行是这样的:

$proxy = Factory::create();

为什么会这样? use Proxy\Factory;不应该正确包含Factory.php文件吗?

更新: autoload.php的内容是:

<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInit60f173139b462a85925faf1b40913ce6::getLoader();

2 个答案:

答案 0 :(得分:1)

我终于开始工作了。我完全搞砸了Symfony项目结构,所以这就是我做的工作:

在shell中运行(在Mac OS X上):

$ symfony new proxy 2.7
$ cd proxy
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar require jenssegers/proxy
$ sudo cp /etc/php.ini.default /etc/php.ini

/etc/php.ini中搜索时区并删除前面的冒号。然后设置date.timezone = "Europe/Berlin"

然后我编辑此文件:src/AppBundle/Controller/DefaultController.php并添加我的代码:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Proxy\Factory;
use Proxy\Response\Filter\RemoveEncodingFilter;

require '/Users/me/dev/symphony/proxy/vendor/autoload.php';

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        /*
        return $this->render('default/index.html.twig', array(
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
        ));
        */
        // Create the proxy factory.
        $proxy = Factory::create();

        // Add a response filter that removes the encoding headers.
        $proxy->addResponseFilter(new RemoveEncodingFilter());

        // Create a Symfony request based on the current browser request.
        $request = Request::createFromGlobals();

        // Forward the request and get the response.
        $response = $proxy->forward($request)->to('http://example.com');

        // Output response to the browser.
        $response->send();
    }
}

然后只需运行服务器:

$ php app/console server:run

然后打开localhost:8000

答案 1 :(得分:0)

尝试在目录结构中向上移动index.phprequire vendor/autoload.php vendor内的vendor dir看起来太奇怪了,你(和任何人)不太可能拥有vendor {{1}}目录中的目录