zendframework 2 - where to save custom library folder - zend-framework

In Zendframework-1, we usually save the customized code under library folder (parallel to application folder) almost using the same folder structure as zend framework (vendor) library to create plugin or extend core library.
In zend framework 2, folder structure is changed. zend vendor core library is moved under Vendor folder and application folder is moved into Module (root) folder.
My question is, which is the best place to save customized plugin/code based library folder in ZF2?
Anyone else have gone through this phase?

Depends on the purpose of your library
Case 1, used by many modules:
Place it in your vendor folder, make sure to be PSR-0 compliant, that makes autoloading easy.
Case 2, used by only one module:
Place it in under modules/your_module/src and edit the Module.phps getAutoloaderConfig() method to have it autoloaded.
....
class Module {
....
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php', // classmap for production usage
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, // your module's files autoloading (development usage and fallback)
'library_namespace' => __DIR__ . '/src/librarys_namespace/potential_subfolder', // your library files autoloading (development usage and fallback). eg: 'acme' => '/src/acme/library' for acme namespace
),
),
);
}
....
Case 3, your library is 3rd party module:
Place it within the vendor folder, for references look at ZfcUser
I think your use-case would most like be case 1, your library modifies behaviour of e.g. the Zend\Mvc\Controller\AbstractActionController or additional plugins.
But if a plugin is only used by one module you'll be better of placing it parallel to your Modules code as described in Case 2.

./vendor if your code has generic purposes (i.e.: Classes like StdClass, ArrayAccess, Iterator, etc...). In short, if those Classes are required for Modules to work, they should be inside vendor.
./module In case your Plugins/Code are meant for a specific purpose (and Standalone), you may evaluate if it's a module or not (i.e.: ZF-Commons 3rd Party Modules/Plugins like ZfcUser)

Related

Typo3: Put modules of different extensions in one Module group in the typo3 backend module list

Currently I'm trying to create a module group in the typo3 backend module list on the left hand side. My group works fine for modules within the same extension. But when I try to add modules from other extensions to it, it simply doesn't work.
I have created this Module group (mainmodule) in the ext_tables.php file in one of my other extensions like this:
/**
* Creates a Backend Module Category
*/
$GLOBALS['TBE_MODULES'] = array_slice($GLOBALS['TBE_MODULES'], 0, 1, true) +
['mainmodule' => ''] +
array_slice($GLOBALS['TBE_MODULES'], 1, count($GLOBALS['TBE_MODULES']) - 1, true);
$GLOBALS['TBE_MODULES']['_configuration']['mainmodule'] = [
'iconIdentifier' => 'module',
'labels' => 'LLL:EXT:' . $_EXTKEY . '/Resources/Private/Language/locallang_myExt.xlf:mlang_key',
'name' => 'mainmodule',
];
I'm trying to use the mainmodule in a different extension as follows:
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule(
'VEN.' . $extKey,
'mainmodule', // Make module a submodule of 'mainmodule'
'randomkey', // Submodule key
'',
...
The module is always created inside its "own" mainmodule.
I have tried all of the solutions given here on stackoverflow and spent hours of trying to solve this issue. I just can't get it to work..
It seems other extensions are loaded before this extensions which defines the new backend module category. So \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule will fail because of the missing category. To check this have a look at the loading order of the extensions in typo3conf/PackageStates.php.
To resolve the issue, add this extension to the constraint in ext_em.conf and require in composer.json to force it's loaded before the other extensions with the dependency to the new backend module category. See https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ExtensionArchitecture/DeclarationFile/Index.html and https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ExtensionArchitecture/ComposerJson/Index.html.
Other solution could be adding the new category in each extension if it does not already exist.

Laravel 4 using vendor classes

I have installed Laravel 4 after using 3, love it.
I used to be able to use the Zend framework as such:
$yt = new Zend_Gdata_YouTube();
for instance
I have used composer to install Zend and everything is installed in the Vendor folder..
Problem:
How to address the individual classes i.e. Zend Gdata etc.
I can't find any documentation on calling classes from a vendor in L4.
Any help is appreciated.
Take a look at your vendor\composer\autoload_classmap.php file. In there you will find a list of all vendor classes that are being autoloaded. I think all classes will have to be called using their full namespaced name.
E.g.
I'm using Zizaco's Entrust package. This is what it looks like in the vendor\composer\autoload_classmap.php file.
'Zizaco\\Entrust\\Entrust' => $vendorDir . /zizaco/entrust/src/Zizaco/Entrust/Entrust.php',
If I wanted to access the Entrust.php class I have to call
$en = new Zizaco\Entrust\Entrust();
Alternatively you could alias certain classes in your app\config\app.php file.
E.g.
'Ent' => 'Zizaco\Entrust\Entrust'
In your case you'll need to do something like this:
$yt = new Zend\namespace\Zend_Gdata_YouTube();

How to use Zend_Loader_Autoloader

I think this is a reasonably easy question however I just don't get autoloading in Zend framework.
Basically, I have the standard Zend project layout with application/models, application/controllers, application/views directories. I have also added an application/forms directory, and the classes that it contains will be named Application_Form_*
How do I register the new directory with the autoloader?
Thanks...
Kim
You don't need to register the new directory with the autoloader. If you create a form it should look something like like this:-
application/forms/Myform.php
class Application_Form_Myform extends Zend_Form
{
public function init()
{
//Put your code here
}
}
You can then instantiate your form like this (in your controller for example):-
$myform = new Application_Form_Myform();
Zend Framework will then autoload the class for you.
http://framework.zend.com/manual/en/learning.quickstart.create-form.html
There is an explanation of Autoloading in Zend Framework in the manual.
An extract from that:-
Zend Framework has borrowed an idea from ยป PEAR, whereby class names have a 1:1 relationship with the filesystem. Simply put, the underscore character ("_") is replaced by a directory separator in order to resolve the path to the file, and then the suffix ".php" is added. For example, the class "Foo_Bar_Baz" would correspond to "Foo/Bar/Baz.php" on the filesystem. The assumption is also that the classes may be resolved via PHP's include_path setting, which allows both include() and require() to find the filename via a relative path lookup on the include_path.
Which basically means that folders don't all need to be registered in the autoloader. It can quite happily find files in folders anywhere under the application or library/Zend folders so long as you follow the naming convention and proper casing.
The default folders under application/ that end with an 's' are special cases specifically dealt with in Zend_Application_Module_Autoloader::initDefaultResourceTypes() and should not be confused with the main autoloading mechanism.
Use $resourceLoader:
$resourceLoader->addResourceTypes(array(
'acl' => array(
'path' => 'acls/',
'namespace' => 'Acl',
),
'form' => array(
'path' => 'forms/',
'namespace' => 'Form',
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model',
),
));
See: http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html

PHPExcel class not found in Zend Autoloader

I am struggling with namespaces in Zend Framework (at least I think it's a namespace issue).
I want to integrate PHPExcel into my Zend project. Relevant file structure is as follows:
/
-library
-ABCD
-PHPExcel
-Zend
-ZendX
-PHPExcel.php
Custom classes work fine, after
Zend_Loader_Autoloader::getInstance()->registerNamespace('ABCD_');
in the bootstrap. Also, those classes are all named ABCD_blahdeblah.
However, doing registerNamespace('PHPExcel_') doesn't help Zend find the appropriate classes. When I try
$sheet = new PHPExcel;
in the controller, I get a "Class not found" error. I am guessing that this is either because classes in PHPExcel aren't named with the namespace prefix, or because the main PHPExcel.php file sits outside of the namespace I've just declared. But the PHPExcel structure demands that it sit in the parent directory of the rest of the class/font/etc files.
Any pointers would be greatly appreciated.
Thanks in advance.
Create an autoloader for PHPExcel and add it to the Zend autoloader stack.
In library/My/Loader/Autoloader/PHPExcel.php:
class My_Loader_Autoloader_PHPExcel implements Zend_Loader_Autoloader_Interface
{
public function autoload($class)
{
if ('PHPExcel' != $class){
return false;
}
require_once 'PHPExcel.php';
return $class;
}
}
And in application/configs/application.ini:
autoloadernamespaces[] = "My_"
Then, in application/Bootstrap.php:
protected function _initAutoloading()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader(new My_Loader_Autoloader_PHPExcel());
}
Then you should be able to instantiate PHPExcel - say, in a controller - with a simple:
$excel = new PHPExcel();
The only sticky part is all of this is how PHPExcel handles loading all its dependencies within its own folder. If that is done intelligently - either with calls like require_once basename(__FILE__) . '/someFile.php' or with its own autoloader that somehow doesn't get in the way of the Zend autoloader - then all should be cool. #famouslastwords
Nowadays composer is a frequently used tool that wasn't so popular back in 2012. Even older projects built in ZF1 can make use of composer and its autoloader.
How to get all your libraries to work without having to add custom autoloaders to your application.ini each time?
Make use of composer's autoloader
First, start with setting up composer.json. Once created, run composer install to gather all required packages and create composer's autoloader.
Now, let's update your project's public/index.php. From now on all requirements that are loaded via composer will be autoloaded.
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Include composer autoloader
require_once __DIR__ . '/../vendor/autoload.php';
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
array( 'config' => APPLICATION_PATH . '/configs/application.ini' )
);
$application->bootstrap();
$application->run();
Try modifying the PHPExcel autoloader:
Add
if (function_exists('__autoload')) {
spl_autoload_register('__autoload');
}
as the first two lines of the Register() method in /Classes/PHPExcel/Autoloader.php, immediately before
return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));
I've had a similar problem with both an exel-librare (phpxls) and a pdf-library (fpdf) and after some different tries I just settled with including the required file from the library manually and go from there. Booth phpxls and fdpd can then handle everything else without interfering with the zend autoloader methods.
A psudo_code example would look like this, where I return a object of the desired class and then can continue to work with that. You could offcourse choose to include things in the constructor and build from that.
<?php
class exelClass{
public function exelFunction(){
require_once 'required_file.php';
$exelObject = new exelObject();
return $exelObject->Output();
}
}
?>
This solution might not be that elegant, but I found that it was the easiest way to enable different types of libraries to co-exist without differnet autoloaders or magic functions interfearing with each other.

Zend Autoloading models issue

Zend framework.
I want to autoload my models classes inside models folder, from within bootstrap class.
These models doesnt actually use any namespace (so I have Ex. User.php file's class named User and so on..).
If I understood correctly I should use the Zend_Loader_Autoloader_Resource and I tried:
function _initLoaderResource()
{
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,//points to the "application" path where resides "models" folder
'namespace' =>''
));
$resourceLoader->addResourceType('models', 'models/');
}
And I receive following 'Zend_Loader_Exception' message:
'Initial definition of a resource type must include a namespace'
My questions are:
Is this the right way to autoload models?
How should I manage resource code that doesn't follow Zend Framework coding standard?
Actually you probably don't want to use the resource autoloader for this, since (as you've discovered) it requires a namespace. The standard autoloader (which loads models from the include path) has an option setFallbackAutoloader which tells ZF that that autoloader should be used for any class not matching a namespace covered by another. So all you need to do is ensure your models directory is on the include path and set this option to true.
You are probably already using the standard autoloader for loading the Zend classes, so you'll probably want to modify your application.ini file to add your model directory to the include path, and then set the fallback option either in application.ini or in your Bootstrap class:
protected function _initAutoloader()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
return $autoloader;
}
Zend Autoloader uses namespaces to make sure you are not using the autoload process, on those classes you don't want. So you would have to choose a namespace for your classes.
You could start your classes with an application specific namespace, or a general one.
namespaces like 'My_' or 'App_' are general, yet for example if your application name is Job Board, you could use namespaces like 'JB_' in your class files.
You may also write your own autoloader (either a totally new one, or by extending the Zend autoloader) and register it as the SPL autoloader to bypass this.
Your class names does not have to follow the Zend Framework naming conventions, just make sure they have a namespace and register the namespace in the autoloader.
Here I attach a piece of my code that registers some resources to be autoloaded. I'm having multiple modules, and each module has a namespace regarding that module name. Please note that since there were many namespaces, I register them all in a loop.
$nameSpaceToPath = array(
'Application' => APPLICATION_PATH,
'Base' => APPLICATION_PATH . '/base',
'Store' => APPLICATION_PATH . '/modules/Store',
'Payment' => APPLICATION_PATH . '/modules/Payment',
'Admin' => APPLICATION_PATH . '/modules/Admin'
);
foreach($nameSpaceToPath as $ns => $path) {
$autoLoaderResource = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => $path,
'namespace' => $ns
)
);
$autoLoaderResource->addResourceType('controller','controllers','Controller');
$autoLoaderResource->addResourceType('model','models','Model');
$autoLoaderResource->addResourceType('mapper','models/mappers','Model_Mapper');
$autoLoaderResource->addResourceType('service','services','Service');
// I'm using _Util_ in the name of my utility classes, I place them in 'utils' directory
$autoLoaderResource->addResourceType('util','utils','Util');
$autoLoaderResource->addResourceType('plugin','plugins','Plugin');
$autoLoaderResource->addResourceType('form','forms','Form');
// I'm using _Exception_ in the name of my module specific exception classes, I place them in 'exceptions' directory
$autoLoaderResource->addResourceType('exception','exceptions','Exception');
$autoLoader->pushAutoloader($autoLoaderResource);
}
When you are defining a resource type by calling:
$autoLoaderResource->addResourceType('service','services','Service');
You are actually telling Zend Autoloader that you have a type 'service' (1st param), which is placed in the directory named 'services' (2nd param), and you are using 'Service' token in the class names to specify classes of this type.
The above code tells Zend Autoloader to search for class Store_Service_Core in the path 'APPLICATION_PATH/modules/store/services/Core.php'.
As you can see I have registered the general 'Application' namespace for the APPLICATION_PATH path. This means that each class, starting with Application_ would be autoloaded from the APPLICATION_PATH. So forexample I have a class named Application_Init which uses some initialization tasks, and now Zend autoloads it from the path APPLICATION_PATH/Init.php.