2
votes

C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\public\index.php:

            <?php
            /**
             * This makes our life easier when dealing with paths. Everything is relative
             * to the application root now.
             */
            chdir(dirname(__DIR__));

            // Decline static file requests back to the PHP built-in webserver
            if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
                return false;
            }

            // Setup autoloading
            require 'init_autoloader.php';

            // Run the application!
            Zend\Mvc\Application::init(require 'config/application.config.php')->run();

If I place a file in the same directory called console.php:

<?php
echo __DIR___
?>

and run:

php console.php

The output is: C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\public

Clearly this seems to be the wrong directory as 'init_autoloader.php' is actually located here: C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial

Also my book says that the line: Zend\Mvc\Application::init(require 'config/application.config.php')->run();

calls the bootstrap() method of the Zend\Mvc\Application. I'm not sure how a call to init() translates to a call to bootstrap() could someone please explain this to me?

My book also says that the call to init takes care of instantiating a new ServiceManager object although I'm not sure how because I see nothing in the bootstrap method of the Application model that has anything to do at all with ServiceManager. Could someone explain this to me?

Thank you for posting...

For reference zf2-tutorial/Module/Application/Module.php

            <?php
            /**
             * Zend Framework (http://framework.zend.com/)
             *
             * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
             * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
             * @license   http://framework.zend.com/license/new-bsd New BSD License
             */

            namespace Application;

            use Zend\Mvc\ModuleRouteListener;
            use Zend\Mvc\MvcEvent;

            class Module
            {
                public function onBootstrap(MvcEvent $e)
                {
                    $eventManager        = $e->getApplication()->getEventManager();
                    $moduleRouteListener = new ModuleRouteListener();
                    $moduleRouteListener->attach($eventManager);
                }

                public function getConfig()
                {
                    return include __DIR__ . '/config/module.config.php';
                }

                public function getAutoloaderConfig()
                {
                    return array(
                        'Zend\Loader\StandardAutoloader' => array(
                            'namespaces' => array(
                                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                            ),
                        ),
                    );
                }
            }
1

1 Answers

2
votes

Clearly this seems to be the wrong directory as 'init_autoloader.php' is actually located here: C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial

The output from your console.php is different because in index.php you'll see this line ...

chdir(dirname(__DIR__));

This effectiveley changes up one directory to C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial which is the root of the application and the same folder in which init_autoloader.php is located.

Also my book says that the line: Zend\Mvc\Application::init(require 'config/application.config.php')->run(); calls the bootstrap() method of the Zend\Mvc\Application.

You are confusing the Zend\Mvc\Application with the skeleton application module named Application. They are not the same thing.

The bootstrapping being referred to by your book is happening here in the code ...

https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Application.php#L247-L261

As you can see it's a static method, which instantiates the ServiceManager and proceeds to set up services before finally bootstrapping the application here ...

https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Application.php#L136-L158

For further reading I'd suggest familiarizing yourself with the MVC layer by reading the docs here

http://framework.zend.com/manual/2.3/en/modules/zend.mvc.intro.html