phiamo / MopaBootstrapBundle

Easy integration of twitters bootstrap into symfony2
http://bootstrap.mohrenweiserpartner.de
711 stars 348 forks source link

InvalidArgumentException: The service definition "mopa_bootstrap.example.navbar" does not exist. #209

Closed jcroll closed 12 years ago

jcroll commented 12 years ago

I am new to the Symfony2 framework so maybe I am missing something but I keep getting the following error when I add {{ mopa_bootstrap_navbar('frontendNavbar') }} to my twig template (which is pretty much a carbon copy of Resources/views/base.html.twig):

InvalidArgumentException: The service definition "mopa_bootstrap.example.navbar" does not exist.
in /home/admintools/development/vendor/symfony/src/Symfony/Component/DependencyInjection/ContainerBuilder.php line 678
at ContainerBuilder->getDefinition() in /home/admintools/development/vendor/symfony/src/Symfony/Component/DependencyInjection/Compiler/ReplaceAliasByActualDefinitionPass.php line 42
at ReplaceAliasByActualDefinitionPass->process() in /home/admintools/development/vendor/symfony/src/Symfony/Component/DependencyInjection/Compiler/ReplaceAliasByActualDefinitionPass.php line 56

I am using Symfony 2.0x and MopaBootstrapBundle v.2.0.x.

I have the KnpMenuBundle menu bundle installed and working correctly.

I have the following in config.yml:

mopa_bootstrap:
    navbar: ~

I find the documentation to be pretty confusing. Any idea what I'm doing wrong?

phiamo commented 12 years ago

first of all do not use a copy of the base template, extend it!

next is you probably copied a wrong service definition.

btw which version of symfony / this bundle are you working with?

BRMatt commented 12 years ago

The documentation is a bit confusing, but it helps to learn a bit about how symfony2 works. Quite a lot of symfony 2 components and bundles try to be as reusable as possible, and therefore it doesn't make sense to hardcode things which other people may want to change. To allow for this change without hacking the bundle itself symfony uses a service container, which is an implementation of the dependency injection pattern. The principle is that instead of a piece of code manually hardcoding a dependency, the service container can generate the dependency and pass it to the code.

Go off and read the symfony service container documentation before reading further.

In this case our dependency is the content of the frontendNavbar. Here are some examples from one of our apps:

services:
    calendar.navbar:
        class: '%mopa_bootstrap.navbar.generic%'
        scope: request
        arguments:
            # first argument: a named array of menues:
            - { leftmenu: @calendar.navbar_main_menu=, rightmenu: @calendar.navbar_right_menu= }
            - {}
            - { title: "Team-Up", titleRoute: "event_list", isFluid: false, fixedTop: true }
        tags:
            - { name: mopa_bootstrap.navbar, alias: frontendNavbar }

This basically tells the service container that you're defining a dependency calendar.navbar which is a class of the type %mopa_bootstrap.navbar.generic% (a value surrounded by percent symbols is a service parameter, usually defined in services.xml or parameters.yml. Parameters make it easier to re-use class names without specifying the fully qualified path each time).

The arguments section states that we're passing 3 associative arrays as arguments to the constructor of the generic navbar class. The first parameter has a key leftmenu whose value is generated by the calendar.navbar_main_menu service. Similarly, rightmenu's value is generated by the calendar.navbar_right_menu service. The rest of the arguments are fairly straightforward literal values (or in symfony config speak, scalars).

The tags section allows a service to tag itself in a specific "list". You can use these lists to get all services that can handle a specific task (e.g. twig uses tags to allow extra bundles to register new helper functions).

What we're essentially doing is:

$frontendNavbar[] = {our navbar};

And then things like MBB can iterate over all items in the frontendNavbar set.

If you were to to take out the navbar_main_menu and navbar_right_menu values in the first argument you'd essentially have the bare minimum to make it "work". To actually add items to those menus, you need to define services that will provide the menus:


    calendar.navbar_main_menu:
        class: KNP\Menu\MenuItem
        factory_service: calendar.navbar_menu_builder
        factory_method: createMainMenu
        arguments: [ '@request' ]
        scope: request
        tags:
            - { name: knp_menu.menu, alias: main }

    calendar.navbar_right_menu:
        class: Knp\Menu\MenuItem
        factory_service: calendar.navbar_menu_builder
        factory_method: createRightSideDropdownMenu
        arguments: [ '@request' ]
        scope: request
        tags:
            - { name: knp_menu.menu, alias: main }

    calendar.navbar_menu_builder:
        class: Bundle\CalendarBundle\Menu\MenuBuilder
        scope: request
        arguments: ['@knp_menu.factory', '@security.context' ]

The first two definitions define dependencies calendar.navbar_main_menu and calendar.navbar_right_menu which will be instances of MenuItem. Because yml is terrible for doing other than basic definitions we're going to use php to construct the menus dynamically. In this case we've created a separate service called calendar.navbar_menu_builder which is an instance of the MenuBuilder class. When these services are requested Symfony will call MenuBuilder::createMainMenu() (to generate calendar.navbar_main_menu) and navbar_right_menu.

These methods should return instances of \KNP\Menu\MenuItem.

Most of our setup was taken from this advanced configuration in the docs, it provides an example implementation of MenuBuilder.

BRMatt commented 12 years ago

(In summary, this isn't a bug you've just not set the bundle up properly)

phiamo commented 12 years ago

thanks a lot, it would be nice to include this explanaition somehow into the docs too :)

jcroll commented 12 years ago

@BRMatt I just wanted to come back in here and thank you for that post as I was able to use it in setting up a basic navbar. It would be nice if your example could be included in the docs as it is helpful.

@phiamo The documentation regarding the navbar is very messy. Perhaps it is acceptable for someone who already has very good experience with the service container but for a newbie such as myself it is a bit of a nightmare. I would actually volunteer to clean it up but I would not want to at this time as I do not have enough experience with the framework or your bundle.

I am going to take the time to list some of my issues regarding the navbar documentation: In Resources/doc/4-navbar-generation.md:

I think these are my major complaints, I took the time to write them down because I hope they might actually be of some use. If I come off as unappreciative of you or your bundle's contribution to the Symfony2 ecosphere I apologize, my intention is simply to make access to your bundle easier for future developers learning the framework.

Thank you for your time.

ftassi commented 12 years ago

Hi guys, I really appreciated @BRMatt explanation on this topic, but still I can't get the point of having a default configuration parameter that referer to a non existent service. Is that something that we can get rid of ?

If navbar service param defaults to mopa_bootstrap.example.navbar a basic mopa_bootstrap.example.navbar should be shipped with the bundle, or if the bundle comes without this service no configuration should refer to it.

Am I missing something maybe ?

paskuale75 commented 11 years ago

thanks for a great bundle ... but I'm going crazy from three days to install and configure ... the guides are a little jagged or confused, knows someone point me in the right path? thanks;) I set the settings from the link http://knpbundles.com/phiamo/MopaBootstrapBundle but I get an error in the "row_wrapper_class: form-group" why ? tnx

phiamo commented 11 years ago

Are you using MopaBootstrapSandboxBundle? In which env does this happen? Can you post confix*.yml s and composer. Jon as well as AppKernel.php

paskuale75 commented 11 years ago

Hi @phiamo, not the sandbox have not installed between the bundles, config.yml:


    version:              ~

    form:

        templating:           MopaBootstrapBundle:Form:fields.html.twig

        horizontal_label_class:  col-lg-3

        horizontal_input_wrapper_class:  col-lg-9

        row_wrapper_class:    form-group

        render_fieldset:      true

        render_collection_item:  true

        show_legend:          true

        show_child_legend:    false

        checkbox_label:       both

        render_optional_text:  true

        render_required_asterisk:  false

        error_type:           ~

        tooltip:

            icon:                 icon-info-sign

            placement:            top

        tabs:

            class:                nav nav-tabs

        popover:

            icon:                 icon-info-sign

            placement:            top

        collection:

            widget_remove_btn:

                attr:

                    class:                btn

                icon:                 ~

                icon_color:           ~

            widget_add_btn:

                attr:

                    class:                btn

                icon:                 ~

                icon_color:           ~

    navbar:

        template:             MopaBootstrapBundle:Navbar:navbar.html.twig

    initializr:

        meta:

            title:                MopaBootstrapBundle

            description:          MopaBootstrapBundle

            keywords:             MopaBootstrapBundle, Twitter Bootstrap, HTML5 Boilerplate

            author_name:          My name

            author_url:           #

            feed_atom:            ~

            feed_rss:             ~

            sitemap:              ~

            nofollow:             false

            noindex:              false

        dns_prefetch:

            # Default:

            - //ajax.googleapis.com

        google:

            wt:                   ~

            analytics:            ~

        diagnostic_mode:      false```
paskuale75 commented 11 years ago

Composer.json here :

{ "name": "symfony/framework-standard-edition", "license": "MIT", "type": "project", "description": "The \"Symfony Standard Edition\" distribution", "autoload": { "psr-0": { "": "src/" } }, "require": { "php": ">=5.3.3", "symfony/symfony": "2.3.", "doctrine/orm": ">=2.2.3,<2.4-dev", "doctrine/doctrine-bundle": "1.2.", "twig/extensions": "1.0.", "symfony/assetic-bundle": "2.3.", "symfony/swiftmailer-bundle": "2.3.", "symfony/monolog-bundle": "2.3.", "sensio/distribution-bundle": "2.3.", "sensio/framework-extra-bundle": "2.3.", "sensio/generator-bundle": "2.3.", "incenteev/composer-parameter-handler": "~2.0", "mopa/bootstrap-bundle": "dev-master", "twbs/bootstrap": "dev-master", "knplabs/knp-paginator-bundle": "dev-master", "knplabs/knp-menu-bundle": "dev-master", "knplabs/knp-menu": "2.0.@dev", "craue/formflow-bundle": "dev-master" }, "scripts": { "post-install-cmd": [ "Incenteev\ParameterHandler\ScriptHandler::buildParameters", "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap", "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache", "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets", "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile", "Mopa\Bundle\BootstrapBundle\Composer\ScriptHandler::postInstallSymlinkTwitterBootstrap" ], "post-update-cmd": [ "Incenteev\ParameterHandler\ScriptHandler::buildParameters", "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap", "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache", "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets", "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile", "Mopa\Bundle\BootstrapBundle\Composer\ScriptHandler::postInstallSymlinkTwitterBootstrap" ] }, "config": { "bin-dir": "bin" }, "minimum-stability": "stable", "extra": { "symfony-app-dir": "app", "symfony-web-dir": "web", "incenteev-parameters": { "file": "app/config/parameters.yml" }, "branch-alias": { "dev-master": "2.3-dev" } } }

paskuale75 commented 11 years ago

... and appKernel.php here : <?php

use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel { public function registerBundles() { $bundles = array( new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), new Symfony\Bundle\SecurityBundle\SecurityBundle(), new Symfony\Bundle\TwigBundle\TwigBundle(), new Symfony\Bundle\MonologBundle\MonologBundle(), new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(), new Symfony\Bundle\AsseticBundle\AsseticBundle(), new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), new Mopa\Bundle\BootstrapBundle\MopaBootstrapBundle(), new Knp\Bundle\MenuBundle\KnpMenuBundle(), new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(), new Craue\FormFlowBundle\CraueFormFlowBundle(), new Framipa\StoreBundle\FramipaStoreBundle(), );

    if (in_array($this->getEnvironment(), array('dev', 'test'))) {
        $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
        $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
    }

    return $bundles;
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
    $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}

}

paskuale75 commented 11 years ago

a screenshot of issue ... mopa_bstrap_issue

phiamo commented 11 years ago

hmm please do a composer.phar update first ... then try a inheriting config: changing you mopa_bootstrap config to:

mopa_boostrap:
    navbar: ~
    form: ~

if using initializer template add these base vars

but not setting too much else, so errors show up where they are and not at config point, then we cna try getting further

paskuale75 commented 11 years ago

tnx @phiamo now the issue is another ;) (screenshot below)

navbar_issue

phiamo commented 11 years ago

did you register a navbar called frontendNavbar ? https://github.com/phiamo/MopaBootstrapBundle/blob/master/Resources/doc/4-navbar-generation.md#generate-a-own-navbar

paskuale75 commented 11 years ago

no i did not configured, but following the guide did not seem necessary, therefore I must read to all files under the path / Resource / doc / *. md? tnx

paskuale75 commented 11 years ago

please have a way to rewrite the guide? I'm going crazy to try this bundle! the sandbox have not installed it because I find him in the example of services (navbar)? tnx

paskuale75 commented 11 years ago

mmm.... I added {% block navbar %}

{% endblock navbar %} in my bundle view .... but where is twitter bootstrap ? I have only this ... minimal_mopa_bootstrap