【问题标题】:Zend Framework index.php. __DIR__ and ServiceMangerZend 框架 index.php。 __DIR__ 和 ServiceManger
【发布时间】:2014-03-26 03:45:35
【问题描述】:

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();

如果我将文件放在名为 console.php 的同一目录中:

<?php
echo __DIR___
?>

然后运行:

php 控制台.php

输出是: C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\public

显然,这似乎是错误的目录,因为“init_autoloader.php”实际上位于此处: C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial

我的书还说: Zend\Mvc\Application::init(require 'config/application.config.php')->run();

调用 Zend\Mvc\Application 的 bootstrap() 方法。我不确定对 init() 的调用如何转换为对 bootstrap() 的调用,有人可以向我解释一下吗?

我的书还说,对 init 的调用负责实例化一个新的 ServiceManager 对象,尽管我不确定如何,因为我在 Application 模型的引导方法中看不到任何与 ServiceManager 有任何关系的东西。有人可以向我解释一下吗?

感谢您发布...

参考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__,
                            ),
                        ),
                    );
                }
            }

【问题讨论】:

    标签: zend-framework2


    【解决方案1】:

    显然这似乎是错误的目录,因为“init_autoloader.php”实际上位于此处:C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial

    console.php 的输出不同,因为在index.php 中,您会看到这一行...

    chdir(dirname(__DIR__));
    

    这有效地将一个目录更改为C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial,这是应用程序的根目录,与init_autoloader.php所在的文件夹相同。

    我的书还说: Zend\Mvc\Application::init(require 'config/application.config.php')->run();调用 Zend\Mvc\Application 的 bootstrap() 方法。

    您将Zend\Mvc\Application 与名为Application 的骨架应用程序模块 混淆了。它们不是同一件事。

    您的书中提到的引导发生在代码中...

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

    如您所见,它是一个静态方法,它实例化 ServiceManager 并继续设置服务,然后最终在此处引导应用程序...

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

    为了进一步阅读,我建议通过阅读此处的文档来熟悉MVC

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

    【讨论】:

      猜你喜欢
      • 2012-12-30
      • 2012-06-14
      • 2014-10-10
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多