Contao-Camp 2024
Ergebnis 1 bis 11 von 11

Thema: Unable to create a "contao-bundle" under contao 4.2.4

  1. #1
    Contao-Nutzer
    Registriert seit
    26.10.2016.
    Beiträge
    4

    Frage Unable to create a "contao-bundle" under contao 4.2.4

    Hi,

    With a fresh install of Contao 4.2.4 standard-edition the composer autoloader doesn't work.

    My basic structure for my moduletest :
    Code:
    vendor/addictic/
    └── moduletest-bundle
        ├── composer.json
        └── src
            ├── AddicticModuletestBundle.php
            └── Resources
                └── contao
                    ├── classes
                    │   └── Moduletest.php
                    ├── config
                    │   ├── autoload.ini
                    │   ├── autoload.php
                    │   ├── config.php
                    │   └── ide_compat.php
                    ├── dca
                    │   └── tl_car.php
                    ├── languages
                    │   └── fr
                    ├── models
                    ├── modules
                    └── templates
                        ├── modules
                        └── views
    
    14 directories, 8 files
    vendor/addictic/moduletest-bundle/composer.json :
    Code:
    {
        "name": "addictic/moduletest-bundle",
        "type": "contao-bundle",
        "description": "Addictic moduletest bundle",
        "license": "LGPL-3.0+",
        "authors": [
            {
                "name": "Addictic",
                "homepage": "https://www.addictic.fr"
            }
        ],
        "autoload": {
            "psr-4": {
                "Addictic\\ModuletestBundle\\": "src/"
            },
            "classmap": [
                "src/Resources/contao"
            ],
            "exclude-from-classmap": [
                "src/Resources/contao/config",
                "src/Resources/contao/dca",
                "src/Resources/contao/languages",
                "src/Resources/contao/templates"
            ]
        }
    }
    vendor/addictic/moduletest-bundle/src/AddicticModuletestBundle.php :
    PHP-Code:
    <?php

    /*
     * This file is part of Contao.
     *
     * Copyright (c) 2016-2017 Addictic
     *
     * @license LGPL-3.0+
     */

    namespace Addictic\ModuletestBundle;

    use 
    Symfony\Component\HttpKernel\Bundle\Bundle;

    /**
     * Configures the Addictic moduletest bundle.
     */
    class AddicticModuletestBundle extends Bundle
    {
    }
    app/appKernel.php add this line:
    [PHP]
    new Addictic\ModuletestBundle\AddicticModuletestBundle (),
    [PHP]

    When i launch
    composer update
    i obtain the following :
    Code:
    > Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
    PHP Fatal error:  Class 'Addictic\ModuletestBundle\AddicticModuletestBundle' not found in /ha.0/contaofresh/www/app/AppKernel.php on line 40
    Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-update-cmd event terminated with an exception
    
    
      [RuntimeException]
      An error occurred when executing the "'cache:clear --no-warmup'" command:
      PHP Fatal error:  Class 'Addictic\ModuletestBundle\AddicticModuletestBundle' not found in /ha.0/contaofresh/www/app/AppKernel.php on line 40
    Same error when i execute app_dev.php :
    PHP-Code:
    ClassNotFoundException in AppKernel.php line 40Attempted to load class "AddicticModuletestBundle" from namespace "Addictic\ModuletestBundle".
    Did you forget a "use" statement for another namespace? 
    Any one i've an idea ?

    Best regard

  2. #2
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.803
    Partner-ID
    10107

    Standard

    Local bundles must be located in the /src folder. Vendor bundles musst be defined in the /composer.json. You can also add local repositories to your /composer.json (see http://marekkalnik.tumblr.com/post/2...from-local-git for example).

  3. #3
    Contao-Nutzer
    Registriert seit
    26.10.2016.
    Beiträge
    4

    Computer

    Thanks your answer is very helpful.

    I've put my bundle in /src/addictic/moduletest-bundle

    In /composer.json i add before require :

    Code:
    "repositories":
     [
    	{
    		"type": "path",
    		"url": "src/addictic/moduletest-bundle"
    	}
    ]
    Always in /composer.json à add in require at the end:
    Code:
    "addictic/moduletest-bundle": "*"
    Now i've a question why my bundle must be located in /src folder because in /vendor it's works too and i don't need of repositories property in this case. Moreover the following symlink is generated :
    Code:
    moduletest-bundle -> ../../src/addictic/moduletest-bundle
    Geändert von adavid (26.10.2016 um 16:47 Uhr)

  4. #4
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.803
    Partner-ID
    10107

    Standard

    If you put your bundle in /src, you do not need to add it to your /composer.json.

  5. #5
    Contao-Nutzer
    Registriert seit
    26.10.2016.
    Beiträge
    4

    Standard

    Nop, if i don't specify the /composer.json my class bundle is not found :

    After a composer update :
    Code:
    > Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
    PHP Fatal error:  Class 'Addictic\ModuletestBundle\AddicticModuletestBundle' not found in /ha.0/contaofresh/www/app/AppKernel.php on line 40
    Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-update-cmd event terminated with an exception
    
    
      [RuntimeException]
      An error occurred when executing the "'cache:clear --no-warmup'" command:
      PHP Fatal error:  Class 'Addictic\ModuletestBundle\AddicticModuletestBundle' not found in /ha.0/contaofresh/www/app/AppKernel.php on line 40

  6. #6
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.803
    Partner-ID
    10107

    Standard

    Zitat Zitat von adavid Beitrag anzeigen
    I've put my bundle in /src/addictic/moduletest-bundle
    The Convention for Bundles is to have a vendor folder starting with an uppercase letter, as well as a bundle folder with camel cases. So it should be
    Code:
    /src/Addictic/ModuletestBundle

  7. #7
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.803
    Partner-ID
    10107

    Standard

    Also you need to change the default /composer.json - it is missing the autoloading of the /src folder (which would be present in the symfony/standard-edition). Change
    Code:
        "autoload": {
            "classmap": [
                "app/AppCache.php",
                "app/AppKernel.php"
            ]
        },
    to
    Code:
        "autoload": {
            "classmap": [
                "app/AppCache.php",
                "app/AppKernel.php"
            ],
            "psr-4": { "": "src/" }
        },
    and then execute
    Code:
    composer dump-autoload

  8. #8
    Maintainer Avatar von xtra
    Registriert seit
    02.07.2009.
    Ort
    Tuebingen
    Beiträge
    2.007
    User beschenken
    Wunschliste

    Standard

    @adavid: You are mixing local bundles with vendor bundles here.

    There are two different kinds of bundles:
    1. local bundles which are to remain part of the application (most likely named AppBundle)
    2. vendor bundles, which are not essentially part of the application but are intended for distribution and reusage in other applications.

    For local bundles, you put them in "src/FooBundle" and add the corresponding section to your projects autoload information.
    Example (all paths from application root):

    /src/FoobarBundle/FoobarBundle.php
    PHP-Code:
    <?php
    namespace FooBar\FoobarBundle;

    class 
    FoobarBundle
    {
    }
    /composer.json (only the relevant parts)
    Code:
    {
        "autoload": {
            "psr-4": {
                "FooBar\\FoobarBundle\\": "src/FoobarBundle"
            }
    }
    For vendor bundles, you have to do something more, I usually use "_external" as base for such modules.

    You now clone/create your bundle in "_external/foobar".

    /_external/foobar/composer.json (only the relevant parts)
    Code:
    {
        "name": "foobar/foobar-bundle"
        "autoload": {
            "psr-4": {
                "FooBar\\FoobarBundle\\": "src"
            }
    }
    /_external/foobar/src/FoobarBundle.php
    PHP-Code:
    <?php
    namespace FooBar\FoobarBundle;

    class 
    FoobarBundle
    {
    }
    /composer.json (only the relevant parts)
    Code:
    {
        "require": {
            "foobar/foobar-bundle": "~1.0@dev",
        },
        "repositories": [
            {
                "type": "path",
                "url": "_external/foobar"
            }
        ],
    }
    Then you can do "composer install" and it will symlink "_external/foobar" to "vendor/foobar/foobar-bundle".

    You may now edit files in "_external/foobar" and have them available in the application immediately (due to symlinking).

    When you have released your bundle (to packagist), you may either omit the repository entry and work directly in "vendor/foobar/foobar-bundle" or leave things as they are.

    Yet again, this is nothing special to neither Contao nor to symfony but simply to the way composer works.
    Bedenke stets: Wenn Du ungenaue oder unzureichende Angaben machst, so koennte dies die Bearbeitung deiner Frage endlos verzoegern (oder sogar dazu fyhren, dass ich zu viel nachdenken muss und die Antwort vergesse!). Kein Support per PN.

  9. #9
    Contao-Nutzer
    Registriert seit
    26.10.2016.
    Beiträge
    4

    Standard

    Finally i take the two solutions. The bundle of the application in /src with the good naming convention

    My reusable bundle in /_external (or other name)

    Thank you both I have a much clearer vision now.

  10. #10
    Contao-Nutzer
    Registriert seit
    03.07.2009.
    Beiträge
    46

    Standard

    Solved
    @xtra
    Hi, Thank you for clarification on bundles. I tried to follow your instruction on creating local bundles, but I am not getting it to work. I did as below.

    1. Created a folder /src/FoobarBundle
    2. /src/FoobarBundle/FoobarBundle.php
    PHP-Code:
    <?php

    namespace FooBar\FoobarBundle;

    use 
    Symfony\Component\HttpKernel\Bundle\Bundle;

    class 
    FoobarBundle extends Bundle
    {
    }
    3. Added to the main /composer.json
    Code:
    {
        "autoload": {
            "psr-4": {
                "FooBar\\FoobarBundle\\": "src/FoobarBundle"
            }
    }
    4. Added to /app/AppKernal.php
    Code:
    $bundles = [ 
    ..
    new FooBar\FoobarBundle(),
    ..
    ]
    5. > composer dump-autoload

    When I do bin/console cache:clear. It throws following errors
    Code:
    PHP Fatal error:  Class 'FooBar\FoobarBundle' not found in /var/www/_Contao4/contao4310/app/AppKernel.php on line 42

    My Questions
    1. Can you please tell me where I am missing something
    2. Assuming it has been working, if I put regular Contao3 module inside folder /src/FoobarBundle/Resources/contao, will it work?



    Solved:
    1. I made a mistake in step 4. Correct entry to /app/AppKernal.php should be
    Code:
    $bundles = [ 
    ..
    new FooBar\FoobarBundle\FoobarBundle(),
    ..
    ]
    2. Yes, it works.
    Geändert von tsarma (31.05.2017 um 11:07 Uhr)

  11. #11
    Contao-Nutzer
    Registriert seit
    11.06.2013.
    Beiträge
    42

    Standard

    @xtra,

    thanks for explaining the vendor packages, unfortunately there are a few questions remaining.
    Your _external is similiar with packagist.org, except bundles are loaded from the local repository, isn't it?

    If I want to have more local bundles, could this be done with a packages.json in which all my bundles are declared? Assuming the following filestructure in _external


    Code:
    bundle1
    	.git
    	src
    		namespace.php (namespace declaration)
    		composer.json (bundle informations, like requirements and dependencies)
    	composer.json (composer install informations)
    bundle2
    	.git
    	src
    		namespace.php
    		composer.json
    	composer.json
    packages.json (for declaration of my bundles in my repository)
    I have the following packages.json

    PHP-Code:
    {
    "packages": {
        
    "vendor/bundle1": {
            
    "dev-master": {
                
    "name""vendor/bundle1",
                
    "version""1.0.0",
                
    "dist": {
                    
    "type""path",
                    
    "url""D:\\_external\\bundle1"
                
    }
            }
        },
        
    "vendor/bundle2": {
            
    "dev-master": {
                
    "name""vendor/bundle2",
                
    "version""1.0.0",
                
    "dist": {
                    
    "type""path",
                    
    "url""D:\\_external\\bundle2"
                
    }
            }
        }
    }

    and the the two Composer.json

    PHP-Code:
    {
        
    "autoload": {
            
    "psr-4": { "vendor\\bundle1""src/" }
        }

    and

    PHP-Code:
    {
      
    "name""vendor/bundle1",
      
    "description""Contao Extension for testing purpose",
      
    "keywords": ["contao""Extension","test"],
      
    "type""contao-module",
      
    "homepage""",
      
    "license""GPL-3.0-or-later",
        
    "authors":[
            {
                
    "name":"John Doe",
                
    "homepage":"",
                
    "role":"Developer"
            
    }
        ],
        
    "support":{
          
    "email""",
            
    "issues":"",
            
    "source":""
        
    },
        
    "require": {
        
    "php"">=5.3",
        
    "contao/core-bundle""~3.5 || ~4.1"
      
    },

    It is a little bit confusing with vendor/bundle in the different files. My goal is to reduce the given information in the json files to the absolutly neccesary minimum. My problem is, that composer gives me a failure:

    Could not find package vendor\bundle1 with stability stable if I try to "composer create-project ", with composer require it seems that he is copying the src Folder.

    Am I right that if I have a require contao/core-bundle in my bundle that contao should be installed, too, when I "composer create-project vendor/bundle1"?
    Geändert von rwinkler (07.08.2018 um 15:19 Uhr)

Aktive Benutzer

Aktive Benutzer

Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)

Lesezeichen

Lesezeichen

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •