PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 6, 2022

[FIXED] Creating a new project and bundle - "Did you forget a 'use' statement for another namespace?"

 January 06, 2022     composer-php, php, symfony     No comments   

Issue

I am trying to create a test project with a test bundle. I get the below error:

Attempted to load class "TestBundle" from namespace "test". Did you forget a "use" statement for another namespace?

I have read all the instructions on the Symfony website and tried many different things but no joy.

test/config/bundles.php

    return [
        Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
        test\TestBundle::class => ['all' => true],
    ];

test/src/TestBundle/TestBundle.php

namespace test\TestBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class TestBundle extends Bundle
{
    public function getPath(): string
    {
        return \dirname(__DIR__);
    }
}

test/src/TestBundle/composer.json

{
    "type": "symfony-bundle",
    "name": "TestBundle",
    "type": "testing building a reusuable bundle",
    "license": "proprietary",
    "minimum-stability": "dev",
    "prefer-stable": true,
    "require": {
        "php": ">=7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "symfony/console": "5.4.*",
        "symfony/dotenv": "5.4.*",
        "symfony/flex": "^1.3.1",
        "symfony/framework-bundle": "5.4.*",
        "symfony/runtime": "5.4.*",
        "symfony/yaml": "5.4.*"
    },
    "require-dev": {
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "test\\TestBundle\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "test\\TestBundle\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*"
    },
    
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "5.4.*"
        }
    }
}

test/composer.json

{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "dev",
    "prefer-stable": true,
    "require": {
        "php": ">=7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "symfony/console": "5.4.*",
        "symfony/dotenv": "5.4.*",
        "symfony/flex": "^1.3.1",
        "symfony/framework-bundle": "5.4.*",
        "symfony/runtime": "5.4.*",
        "symfony/yaml": "5.4.*",
    "symfony/yaml": "5.4.*",   
    "test/TestBundle": "*"
    },
    "require-dev": {
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "5.4.*"
        }
    }
}

Solution

Since you are not installing the "bundle" using composer, the autoloader does not get generated, and the class pertaining to your new package will never be found.

The bundle's composer.json file is irrelevant, since again, you are not installing the package through composer. Thus, it's never read.

But you can instruct composer to generate autoloader files that take this new bundle into account.

Let's say that this is your directory structure:

- config     <--- where bundles.php resides, among other files
- src        <--- the application code that "consumes" your bundle,  
- testBundle <--- This is where you have your bundle's code. 
- vendor     
- etc

Note that your bundle's code is not on the same directory than the rest of the application code.

Now in your application composer.json file you should need to add something like:

{
    "autoload": {
        "psr-4": 
            {
            "App\\": "src/",
            "test\\TestBundle\\": "testBundle/"
        }
    }
}

After doing this, you should re-dump the autoloader (composer dump-autoload) and the files should be discoverable.

(Note, in your question, you say that the namespace for TestBundle is test\TestBundle, but then in bundles.php you try to use test\TestBundle::class. One of those things is wrong, it's either namespace test;, or test\TestBundle\TestBundle::class).



Answered By - yivi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing