duanmie9741
duanmie9741
2012-08-18 14:17
浏览 27
已采纳

正在加载第三方库:未找到

I am trying to include a third party library into my Symfony 2 project as explained here. However, I keep getting the error message Fatal error: Class 'Sprain_Images' not found in /src/MyProject/MyBundle/Controller/BackendController.php on line 267.

Here is what I did:

I put a third party class into the src folder (not directly in vendors because this class is not available to be loaded by deps).

#Directory structure
-src
  -MyProject
  -vendor
     -sprain
       -lib
         -Images
           -src
           Images.php

Then I created the class to be used:

 # /src/vendor/sprain/lib/Images/Images.php
 require_once __DIR__.'/src/class.Images.php';
 class Sprain_Images extends Images {
 }

I also registered the prefix in autoload.php:

# /app/autoload.php
$loader->registerPrefixes(array(
  'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
  'Twig_'            => __DIR__.'/../vendor/twig/lib',
  'Sprain_'          => __DIR__.'/../src/vendor/sprain/lib',
));

And eventually I called the class in my controller:

# /src/MyProject/MyBundle/Controller/BackendController.php
$image = new \Sprain_Images();

However the class is not being found. Where did I make the mistake?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将第三方库包含到我的Symfony 2项目中,如解释这里。 但是,我一直收到错误消息致命错误:在第267行的/src/MyProject/MyBundle/Controller/BackendController.php中找不到类'Sprain_Images' < p>这是我做的:

我将第三方类放入src文件夹(不是直接在供应商中,因为这个类不能由deps加载)。

  #Directory structure 
-src 
 -MyProject 
 -vendor 
 -sprain 
 -lib 
 -Images 
 -src 
 Images.php 
 <  / code>  
 
 

然后我创建了要使用的类:

 #/ src / vendor / sprain / lib / Images / Images  .php 
 require_once __DIR __。'/ src / class.Images.php'; 
类Sprain_Images扩展图像{
} 
   
 
 

我还注册了前缀 在autoload.php中:

 #/ app / autoload.php 
$loader -> registerPrefixes(array(\''Twig_Extensions_'=&gt; __DIR __。'/。  ./vendor/twig-extensions/lib',
'Twig_'=&gt; __DIR __。'/ .. / vendor / twig / lib',
'Sprain_'  =&GT;  __DIR __。'/ .. / src / vendor / sprain / lib',
)); 
   
 
 

最后我在我的控制器中调用了这个类:

 #/ src / MyProject / MyBundle / Controller / BuckendController.php 
nnmage = new \ Sprain_Images(); 
   
 
  

但是没有找到班级。 我在哪里弄错了?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsmlf1207915
    dsmlf1207915 2012-08-18 14:30
    已采纳

    The class Sprain_Images should be in src/vendor/sprain/lib/Sprain/Images.php.

    You can read more about the PSR-0 standard : https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md#underscores-in-namespaces-and-class-names

    点赞 评论
  • doushouhe7072
    doushouhe7072 2013-02-15 17:34

    You just need to modify your composer.json file for the autoload value:

    http://getcomposer.org/doc/04-schema.md#autoload

    //composer.json in your symfony 2.1 project
    "autoload": {
         "psr-0": { 
             "": "src/", 
             "YourLibrary": "src/location/of/lib"
         }
    },
    

    And then in your controller for example:

    
    namespace Acme\UserBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use YourLibrary\FolderName\ClassName.php;
    
    class DefaultController extends Controller {
    
    /**
     * @Route("/")
     * @Template()
     */
    public function indexAction()
    {
       $lib = new ClassName();
       $lib->getName();
    
       return array('name' => $name);
    }
    
    }
    
    
    点赞 评论

相关推荐