Zend Framework 2 - 如何使用外部库

2021-12-29 00:00:00 php zend-framework zend-framework2

我想将我的自定义类Authentication.php"添加到我的项目中,但我不明白该怎么做?

I want to add my custom class "Authentication.php" to my project but I don't understand how I have to do it ?

我已经阅读了很多关于外部库的操作方法,但没有任何效果.

I have read many howto about the external libs but nothing work.

ZendFramework/module/Firewall/Module.php

ZendFramework/module/Firewall/Module.php

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(
            'ZendLoaderStandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                    'MyNamespace' => __DIR__ . '/../../vendor/MyNamespace/lib/MyNamespace',
                ),
            ),
        );
    }
}

ZendFramework/vendor/MyNamespace/lib/MyNamespace/Authentication.php

ZendFramework/vendor/MyNamespace/lib/MyNamespace /Authentication.php

<?php

class Authentication {

public function test()
{
     die('Works fine');
}


}

?>

如何在控制器中调用外部库.

How I can call my external lib in my controllers.

非常感谢!

推荐答案

我是这样尝试的:

1)

//module/Application/Module.php
public function getAutoloaderConfig()
{
    return array(
        'ZendLoaderStandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                'Mynamespace' => __DIR__ . '/../../vendor/Mynamespace',
            ),
        ),
    );
}

2)

//vendor/Mynamespace/MyClass.php
namespace Mynamespace;
class MyClass
{
    //...
}

3) 我使用它,例如在我的控制器中:

3) I use it, for example in my controller:

use ZendMvcControllerAbstractActionController;
use MynamespaceMyClass;
class AdminController extends AbstractActionController
{
    public function indexAction()
    {
        $myclass = new MyClass();
    }
}

相关文章