zendframework / zend-navigation

Navigation component from Zend Framework
BSD 3-Clause "New" or "Revised" License
20 stars 25 forks source link

Quick Start guide wrong, twice #43

Closed RonnieHuibregtse closed 7 years ago

RonnieHuibregtse commented 8 years ago

The quick start guide is wrong. First (small) issue, it has a very obvious syntax error https://docs.zendframework.com/zend-navigation/quick-start/. Look at the array( part in the first code block.

The second issue is worse, and it is plain wrong and its one of the weirdest parts of the navigation module in my opinion. It says to use <?= $this->navigation('default')->menu() ?>, but you will get an error then. You have to use <?= $this->navigation('navigation')->menu() ?> instead.

froschdesign commented 8 years ago

The second issue is worse, and it is plain wrong…

Yes it's worse, but not the documentation is wrong – the code is wrong!

<?= $this->navigation('navigation')->menu() ?>

Call the navigation helper with a parameter "navigation"?! Really strange! 😕

novrm commented 7 years ago

Also wrong. <?= $this->navigation('Zend\Navigation\Default')->menu() ?> Must be: <?= $this->navigation('Zend\Navigation\Default')->menu() ?>

weierophinney commented 7 years ago

Single backslashes are fine here, as the characters following would not create escape sequences. (Generally, only lowercase characters are used to define escape sequences.)

On Oct 28, 2016 4:55 PM, "novrm" notifications@github.com wrote:

Also wrong. <?= $this->navigation('Zend\Navigation\Default')->menu() ?> Must be: <?= $this->navigation('Zend\Navigation\Default')->menu() ?>

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-navigation/issues/43#issuecomment-257038472, or mute the thread https://github.com/notifications/unsubscribe-auth/AABlV31U5oVMMPziyJT0FtFYgcTjVuYYks5q4m9TgaJpZM4Jale_ .

novrm commented 7 years ago

Ok, weierophinney. Look here. The SERVICE_PREFIX = 'Zend\\Navigation\\' has two backslashes. If apply in code NavigationAbstractServiceFactory - you must have two backslashe in name of navigation. For example name 'Zend\Navigation\Special' calls error:

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Unable to resolve service "ZendNavigationSpecial" to a factory; are you certain you provided it during configuration?'...

froschdesign commented 7 years ago

@novrm

you must have two backslashe in name of navigation

The same here: changing the documentation for strange parameters? No, the code is wrong!

froschdesign commented 7 years ago

@novrm I can not reproduce the your problem. A have tested zend-mvc with version 3.0.4 and zend-navigation with 2.8.1. Which versions do you use?

novrm commented 7 years ago

@froschdesign Code below not work in my blog:

{% use 'layout/body/header/bottom_navbar/header' %}
{% use 'layout/body/header/bottom_navbar/collapse' %}

{% block layoutBodyHeaderBottomNavbar %}
<div class="navbar navbar-inverse navbar-default">
    <div class="container-fluid">
        {% block layoutBodyHeaderBottomNavbarHeader %}{{ parent() }}{% endblock layoutBodyHeaderBottomNavbarHeader %}
        <div class="collapse navbar-collapse">
        {% block layoutBodyHeaderBottomNavbarCollapseNav %}
            {{ navigation('Zend\Navigation\layoutBodyHeaderBottomNavbarCollapseNav').menu().escapeLabels(false)|raw }}
        {% endblock layoutBodyHeaderBottomNavbarCollapseNav %}
        </div>
    </div>
</div>
{% endblock layoutBodyHeaderBottomNavbar %}

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Unable to resolve service "ZendNavigationlayoutBodyHeaderBottomNavbarCollapseNav" to a factory; are you certain you provided it during configuration?' in F:\xampp\htdocs\blog\vendor\zendframework\zend-servicemanager\src\ServiceManager.php:670 Stack trace: #0 F:\xampp\htdocs\blog\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(746): Zend\ServiceManager\ServiceManager->getFactory('ZendNavigationl...') #1 F:\xampp\htdocs\blog\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(195): Zend\ServiceManager\ServiceManager->doCreate('ZendNavigationl...') #2 F:\xampp\htdocs\blog\vendor\zendframework\zend-view\src\Helper\Navigation\AbstractHelper.php(257): Zend\ServiceManager\ServiceManager->get('ZendNavigationl...') #3 F:\xampp\htdocs\blog\vendor\zendframework\zend-view\src\Helper\Navigation\AbstractHelper.php(529): Zend\View\Helper\Navigation\AbstractHelper->parseContainer('ZendNavigationl...') #4 F:\x in F:\xampp\htdocs\blog\vendor\twig\twig\lib\Twig\Template.php on line 222

I have

        "php": "^5.6 || ^7.0",
        "zfcampus/zf-development-mode":           "^3.0",
        "zendframework/zend-component-installer": "^1.0 || ^0.3 || ^1.0.0-dev@dev",
        "zendframework/zend-mvc":                 "^3.0.1",
        "zendframework/zend-cache":               "^2.7.1",
        "zendframework/zend-db":                  "^2.8.1",
        "zendframework/zend-mvc-form":            "^1.0",
        "zendframework/zend-json":                "^3.0",
        "zendframework/zend-log":                 "^2.9",
        "zendframework/zend-mvc-console":         "^1.1.10",
        "zendframework/zend-mvc-i18n":            "^1.0",
        "zendframework/zend-mvc-plugins":         "^1.0.1",
        "zendframework/zend-psr7bridge":          "^0.2.2",
        "zendframework/zend-session":             "^2.7.1",
        "zendframework/zend-servicemanager-di":   "^1.0",

For 'service_manager'

'service_manager' => [
    'abstract_factories' => [
        /**
         * NavigationAbstractServiceFactory.
         *
         * @link https://github.com/zendframework/zend-navigation/tree/master/doc/book
         */
        \Zend\Navigation\Service\NavigationAbstractServiceFactory::class,
    ],
];
froschdesign commented 7 years ago

@novrm Please have a look at the stack trace and you will see, the backslashes are missing:

Zend\View\Helper\Navigation\AbstractHelper->parseContainer('ZendNavigationl...')

Change your Twig template:

{{ navigation('Zend\\Navigation\\layoutBodyHeaderBottomNavbarCollapseNav').menu().escapeLabels(false)|raw }}

This is not a Zend Framework problem and not needed with zend-view. The double backslashes are only used in a Twig template!

novrm commented 7 years ago

@froschdesign You are right. I know as write correct code with Zend Navigation, but... If follow to Zend Navigation guide - we are find not correct code:

There is one important point to know when using NavigationAbstractServiceFactory:
The name of the service in your view must start with Zend\Navigation\ followed by the
name of the configuration key.
This helps ensure that no naming collisions occur with other services.
The following example demonstrates rendering the navigation menus for the named
default, special and sitemap containers.
<!-- in your layout -->
<!-- ... -->

<body>
    <?= $this->navigation('Zend\Navigation\Default')->menu() ?>

    <?= $this->navigation('Zend\Navigation\Special')->menu() ?>

    <?= $this->navigation('Zend\Navigation\Sitemap')->menu() ?>
</body>
<!-- ... -->

This code not work, becose SERVICE_PREFIX = 'Zend\\Navigation\\'. The code above can be correct if will be SERVICE_PREFIX = 'ZendNavigation'. It is not most heavy problem. I not understand why you are not understand me?

froschdesign commented 7 years ago

This code not work, becose SERVICE_PREFIX = 'Zend\Navigation\'.

This is not true. The code works. Again, I have tested the complete scenario with zend-mvc (3.0.4) and zend-navigation (2.8.1).

The code above can be correct if will be SERVICE_PREFIX = 'ZendNavigation'.

No, because it will break all code with zend-view:

Fatal error: Uncaught Zend\ServiceManager\Exception\ServiceNotFoundException:
Unable to resolve service "Zend\Navigation\Default" to a factory;
are you certain you provided it during configuration?

The documentation is correct. The usage in Twig is different.

novrm commented 7 years ago

You are right. I used the debug in controller. Both the cases work correctly.

$renderer = $this->getServiceManager()->get('ViewRenderer');

$menu = $renderer->navigation('Zend\\Navigation\\layoutBodyHeaderBottomNavbarCollapseNav')->menu()->escapeLabels(false)->render();
\Zend\Debug\Debug::dump($menu);

$menu = $renderer->navigation('Zend\Navigation\layoutBodyHeaderBottomNavbarCollapseNav')->menu()->escapeLabels(false)->render();
 \Zend\Debug\Debug::dump($menu);

The usage in Twig is different. But... My view - Twig render a template as in 'use strict' mode... A bit strange to write in framework "Zend\\Navigation\\" and apply in application "Zend\Navigation\"...

froschdesign commented 7 years ago

It says to use <?= $this->navigation('default')->menu() ?>, but you will get an error then.

Fixed with zend-view 2.9.0

An update for composer.json is on the way.