0
votes

So I have a setup of the Zend Framework 2 skeleton, and I want to add a class that I have made, file.class.php , and I want to use it in an action controller.

  1. Where should this file go?
  2. Can I set an autoload? I don't want to use require_once in my action
2

2 Answers

0
votes

The best way is to respect the PSR-0 standards:

You can add your file to /module/Application/src/Application/Service/Classname.php Add the correct namespace and classname to your file:

<?php
namespace Application\Service;

class Classname
{
}

You can then use it in your controller as:

$obj = new Application\Service\Classname;

There are even better ways to add code to ZF2, check out this good introduction to the ServiceManager.

0
votes

in your Bootstrap add the following to the _initAutoload():

 Zend_Loader_Autoloader::getInstance()->registerNamespace('name');

Zend Framework: Autoloading a Class Library