beingalex
beingalex

Reputation: 2476

Zend Framework 2 use my own Library and namespace confusion

I am trying to create a library in ZF2 and really finding it tedious and difficult. My library will be used to perform tasks on images, no front end output. Having read as much as I can I have done the following process.

  1. Created:

    vendor/mycompany/mylibrary/library/mylibrary/mylibrary.php
    
  2. Contents of mylibrary.php:

    <?php
    
    namespace mylibrary;
    
    class mylibrary {
    
    }
    
  3. Added:

    "autoload": {
        "psr-0":  {"mylibrary\\":"vendor/mycompany/mylibrary/library/mylibrary/"}
    }   
    

    to composer.json.

  4. Ran composer:

    `sudo php composer.phar update`
    

    and had no errors and autoload_namespaces.php gets updated with the new namespace:

    return array(
        'mylibrary\\' => array($vendorDir . '/mycompany/mylibrary/library/mylibrary'),
        'Zend\\' => array($vendorDir . '/zendframework/zendframework/library'),
        'ZendXml' => array($vendorDir . '/zendframework/zendxml/library'),
        'ZendPdf\\' => array($vendorDir . '/zendframework/zendpdf/library'),
    );
    
  5. Added:

    use mylibrary;

    to IndexController.php

    and tried to instantiate the class:

    public function indexAction()
    {
    
        $mylibrary = new mylibrary();
        // .... etc etc 
    

I get an error:

Fatal error: Class 'mylibrary' not found in /the/root/to/my/site/module/Application/src/Application/Controller/IndexController.php on line 22

Please could somebody have a look at what I have done and maybe tell me how I can instantiate my class without this error?

Thanks in advance

Upvotes: 2

Views: 796

Answers (3)

Sergio Rinaudo
Sergio Rinaudo

Reputation: 2363

I advice you to use ZFTool to create a new module (this is how a library is called in ZF2), assuming you are using Git and you have Git bash installed, you can do the follow

"require": {
    "php": ">=5.3.3",
    "zendframework/zendframework": "2.3.*",
    "zendframework/zftool": "dev-master",
},
  • Run php composer.phar update.

  • To create a new module in your project, from the console, cd to the project root and run

./vendor/bin/zf.php create module MyModule

or type -v to see all available options

./vendor/bin/zf.php -v

Doing so your module will be created with the correct structure and automatically registered to the application. It will be created inside the application module dir.

  • To make sure everything works you can create a simple route and controller, so open YourZFSkeleton/module/YourNewModule/config/module.config.php and add the follow

    namespace MyNewModule; 

    return array(
        'view_manager' => array(
            'template_path_stack' => array(
                __NAMESPACE__ => __DIR__ . '/../view',
            ),
        ),

        'controllers' => array(
            'invokables' => array(
                __NAMESPACE__ . '\Controller\Index' => __NAMESPACE__ . '\Controller\IndexController',
            ),
        ),

        'router' => array(
            'routes' => array(
                __NAMESPACE__ => array(
                    'type' => 'Literal',
                    'priority' => 1000,
                    'options' => array(
                        'route' => '/example',
                        'defaults' => array(
                            'controller' => __NAMESPACE__ . '\Controller\Index',
                            'action'     => 'index',
                        ),
                    ),
                ),
            ),
        ),
    );

And the IndexController.php within the controller dir of your module


    namespace MyNewModule\Controller;

    use Zend\Mvc\Controller\AbstractActionController,
        Zend\View\Model\ViewModel;

    class IndexController 
        extends AbstractActionController
    {
        public function indexAction() 
        {
            return new ViewModel();
        }
    }

And the view script MyNewModule/view/my-new-module/index/index.phtml

Hello world!

You can see it in action visiting

http://yourSiteUrlOrVhost/example

I didn't test this code, may contain errors or omissions :/

As already adviced, never edit anything inside the vendor dir, it contains dependencies that are automatically downloaded by composer.

If you like, you can create a new repo for your module and then add it ad dependency using composer.

Hope this helps!

Upvotes: 1

Crisp
Crisp

Reputation: 11447

The short answer is your mylibrary class is in the mylibrary namespace and therefore your use statement should be changed to reflect that ...

use mylibrary\mylibrary;

The alternative is to omit the use statement and use the FQCN when instantiating your class

$mylibrary = new \mylibrary\mylibrary();

Upvotes: 1

Tim Fountain
Tim Fountain

Reputation: 33148

You shouldn't be manually creating files in vendor. If you want to be able to share your library with other applications, create the file(s) along with an appropriate composer.json. Put the library in its own source control repository, and then install it using composer into your main application.

Upvotes: 0

Related Questions