Zend Framework 2-如何使用外部库 [英] Zend Framework 2 - How to use an external library

查看:74
本文介绍了Zend Framework 2-如何使用外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将自定义类"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 ?

我已经阅读了很多关于外部库的howto,但是没有任何作用.

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

ZendFramework/供应商/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(
        'Zend\Loader\StandardAutoloader' => 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 Zend\Mvc\Controller\AbstractActionController;
use Mynamespace\MyClass;
class AdminController extends AbstractActionController
{
    public function indexAction()
    {
        $myclass = new MyClass();
    }
}

这篇关于Zend Framework 2-如何使用外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆