13

i followed this doc to install SonataMediaBundle but i got this error:

PHP Fatal error:  Class 'Application\Sonata\MediaBundle\ApplicationSonataMediaBundle' not found in /var/www/znata.com/app/AppKernel.php on line 47

After using the sonata command t generate the app:

php app/console sonata:easy-extends:generate SonataMediaBundle

new directory was generated under:

apps/Application/Sonata/MediaBundle

everything was done but when i registred the generated application in my AppKernel.php i got that error.

public function registerbundles()
{
    return array(
        ...
        new Application\Sonata\MediaBundle\ApplicationSonataMediaBundle(),
        ...
    );
}

Have you any idea how to fix this issue ?

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
skonsoft
  • 1,786
  • 5
  • 22
  • 43

7 Answers7

9

By default project root directory is not in the autoload path, only "src" dir. You can use

php app/console sonata:easy-extends:generate --dest=src SonataMediaBundle

to generate bundle in the src or simple copy it to the src.

Vyacheslav Enis
  • 1,676
  • 12
  • 17
6

After debug this problem, i found that the namspace Application is not registred.

In SF2.0, the documentation said that we should register this namespace like:

<?php
$loader->registerNamespaces(array(
    ...
    'Application'   => __DIR__,
    'Imagine'       => __DIR__.'/../vendor/imagine/lib',
    'Gaufrette'     => __DIR__.'/../vendor/gaufrette/src',
    'Buzz'          => __DIR__.'/../vendor/buzz/lib',
    ...
));

but in SF2.1 they did talked about this.

So i registred the namespace Application in autoload.php and it works fine.

so, the autoload.php look like this:

<?php

// file: app/autoload.php

use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';

//custom for Application
$loader->add("Application", __DIR__);


// intl
if (!function_exists('intl_get_error_code')) {
    require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';

    $loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;

With this new config everything is fine.But in SF2.0, they talked also about "Imagine", "Guffrette" and "Buzz" namespaces. So perhapes, when using them, we should register them also like Application namespace.

I hope that this helps you.

skonsoft
  • 1,786
  • 5
  • 22
  • 43
1

Using composer I did this in composer.json: "autoload": { "psr-0": { "": "src/", "Application": "app/" } },

I added the mapping "Application": "app/". And then run composer update

This generated extra autoloading needed.

Loial
  • 181
  • 3
  • 5
1
  1. inside your composer.json file, have:

    "autoload": {
        "psr-4": {
            "AppBundle\\": "src/AppBundle",
            "Application\\": "src/Application"
        },
    }
    
  2. do a simple:

    composer dump-autoload
    

    to re-generate the autoload files.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
  • thanks perfect, but i prefer keeping bundle logic : `"Application\\": "src/Application" --> "Application\\Sonata\\UserBundle\\": "src/Application/Sonata/UserBundle"` – William Rossier Apr 07 '18 at 18:03
0
new Application\Sonata\MediaBundle\MediaBundle(),

or

new Application\Sonata\MediaBundle\SonataMediaBundle(),
meze
  • 14,975
  • 4
  • 47
  • 52
0

As skonsoft mentioned, you need to load the namespaces in autoload.php. I had the same issue with FOQ.Elastica and resolved it by adding the following.

$loader->add('FOQ', __DIR__.'/../vendor/bundles');

CuberChase
  • 4,458
  • 5
  • 33
  • 52
Mahen
  • 1
0

You can also use your app namespace prefix so the package falls under your namespace

bin/console sonata:easy-extends:generate --dest=src SonataMediaBundle --namespace_prefix=App

Sumeet
  • 1,683
  • 20
  • 27