Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

php - Zend Framework 2 - How to use an external library

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

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

<?php

class Authentication {

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


}

?>

How I can call my external lib in my controllers.

Thanks you very much !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I try like this:

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) I use it, for example in my controller:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...