I am writing three applications (with 1 module each) in Zend Framework 2 that eventually come together as a unified portal with same look and feel. I plan to write some common code that can be shared between these three apps.
Ex: Couple of classes to programatically create a datagrid, Couple of classes to programatically create a form, etc..
Can you suggest an ideal location to put all this code, so I can just plug and play in my modules when required?
Thanks
You can create a module with all the common code in the src folder, PortalCommon/src/PortalCommon for instance. You can then let the 3 modules depend on that module:
namespace Module1;
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
class Module implements DependencyIndicatorInterface
{
public function getModuleDependencies()
{
return array('PortalCommon');
}
...
}
Another option is to create a module for all the common functionality. So in your case you might get a datagrid module and a form module.
You can create a separate module : Jake\Common or Jake\Core in a git repo.
Register this repo on Packagist or on a personnal Satis instance if it is private.
You can then include it as a dependency in your composer.json and it will be downloaded in vendor.
Related
I have created a set of class files that helps to create the route configuration array. I, however, do not know where to put the source files. I browsed around a little and found some answers that suggested I put up the code in packagist and install it via composer, but my classes are relatively small. So, I wanted to ask if there is another way to do it. The files must be accessible in all the modules.
Your application code is organised into modules and lives in module/<Modulename>/src/. If the code is something you might want to reuse in other applications, then it might make more sense to have it as a standalone library that you install via. Composer.
You're code is accessibly through all the application if it is configured in the composer.json's autoload section.
Code structure in ZF is quite simple. You have your modules and in every module you have an src directory. This src directory should have its own namespace. And there you can place your custom route configurator under this namespace, like 'ModuleName\RouteConfigurator'.
But if you use this logic through multiple modules, I suggest you to create a separate module for it. In this case, after a long road, you may consider creating a separate composer package from it. But it's not necessary.
If you're not familiar with defining modules, please read the zend-modulemanager's documentation (https://docs.zendframework.com/zend-modulemanager/intro/)
I am new to yii and using version 2. I developed a project which uses a library (I coded) for parsing a CAD model file, extract vertexes, edges, faces etc, finally combine those entities to display the model and then extract features. It displays the model using the three.js library. A user has to be signed-in in order to upload the model file and do all this stuff. I would like to refactor the app into version 2 using the yii 2 framework. So I would like to know how I can merge the feature recognition library into the yii app, should I create a Components, Extension or Module or use other means?
The library is coded in plain OOP. I access it by requiring an init.php file which requires about 20 library class files. If I create a module for the library the view will draw the model but where do the parser and feature extractor go in the model or controller files?
Thanks in advance.
In Yii 2 basically everything is a component.
Extension is the term usually used for the vendored components (or modules) available at packagist.org (or similar) so if you are not planning to release it outside you don't have to worry about extension-ing it.
Now for the component-module debate - module is a component that allows you to use it's own controllers structure so if you need something like this - use module. In any other case simple component is just fine.
As our company starts using Zend Framework as the base framework for most of our projects, we want to share some common elements across all our projects. I talk about things like:
An implementation of a model (based on doctrine2)
RBAC for the model, including user, group, role models
A xml-based templating engine for ajax backend interfaces
(you name it) ...
Basically, all things to put "zend on rails" and get going. What is the best way to package these components? I see two possibilities:
As modules
We include the necessary functions as separate modules into the modules folder.
Pro:
We can set routes and execute code, which is good for many modules (imaginary example: a paypal module needs some kind of callback url. If our module can set it up on its own, no configuration from the "project developer" is needed).
We can provide real functionality (like the user administration) out of the box
We have a bootstrap to set up autoloading and doctrine etc.
Con:
Bad place? Interferes with the users project
A little harder to share between projects (git submodules instead of classpath)
In the library folder
We put it in the library folder and point the classpath to it.
Pro:
Clean solution
Sharing across projects
Con:
Bootstrap has to be explicitly called
No direct routing or actions - everything has to be proxied through the concrete project
So, how do you solve this? Where do you put your reusable, general purpose stuff in zf?
I think you should use both approaches.
When developing "library-like" code, as in kind of "infrastructure" classes and other things that are reusable (like ZF's own components, Doctrine 2's components etc.), you can put them into the library directory. (or its own entirely separate project)
When developing actual ZF modules (like an auth module for example), then format the code around the ZF module structure.
I think by using this kind of approach you get all the benfits you listed, and pretty much none of the cons :)
As one additional idea, if you develop your architecture parts as "services", you could even keep them running as their own web service endpoints.
The challenge:
I want to modularize my library folder in my zend framework app. This is fine, if you want to put everything in the same namespace such "App_." But the problem comes in when you have a dozen packages such as a SignUp package, ACL Package, Navigation Package, Foo Package etc. Now each packages has some view helpers, some controller plugins, some action helpers plus some other base classes. You could add each view helper path individually, but that could junk up your application.ini file/ bootstrap.
So the question is, is anyone aware of 'Plugin Package' loader for ZF?
To clarify, it would be nice to have a resource plugin that you pass the package name, it adds the namespace, registers some default options like helper paths and then you can configure it to add helpers to Action Helper brokers. Each plugin package might have to have its own ini file or an init class that where the programmer could initialize the plugin package. Any thoughts or knowledge of something like this would be appreciated.
Your question is difficult to find a solution to. ZF doesn't seem to be used in the manner you wish.
For example you want the following packages
Navigation
ACL
Sign Up
These are all completely separate and don't all 'plugin' to ZF in a similar manner.
Navigation needs to be stored and built for each request that needs the package, the navigation object then needs to be used in Zend_View objects or perhaps not. What happens when the navigation is referenced in a view but it doesn't exist?
The ACL package is so specific, it integrates into ZF in a lot of different ways, it needs a Zend_Controller_Plugin, it needs a way of meaningful way of storing and building the Zend_Acl object for querying on an application to application basis.
Sign Up needs a controller, an action and a form which is passed to Zend_View and the form needs to be process. This then needs to be plugged into your ACL object, presumably a database and perhaps various other parts of your site it it requires more specific permissions that fall outside of the use for ACL
Its not impossible to do what you want, but there must be a better way. It almost sounds like your trying to build a CMS with optional, plug-able packages?
The ACL issue I have resolved by having a Library of controllers, helpers, models, forms, etc. A Zend_Controller_Plugin runs and attempts to log in the user, this plugin is run for every app I create, it works well uses an ACL object format which I have used for a while.
For Sign Up I have a RegisterController in my Library, if my application requires registration it has its own RegisterController which extends the RegisterController in the Library. If the application doesn't need registration then it doesn't have its own RegisterController.
I hope that helps, I really think that doing this in the abstracted way suggested isn't worth it and will never be so solid and tight you can truly rely on it because each application is specific.
Generally, in this case you should use modules which are already modularized instead of modularizing the library.
modules/
signup/
models/
plugins/
The default module resource autoloader is configured by default to load plugins:
See: http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html:
If you really need to modularize the library, the customized autoloader will be the best solution (there is an easy to extend abstract autoloader).
You may also consider separate library for each module, added to your include path (not recommended, because it will slow down the app):
modules/
signup/
models/
library/
I devlopp web applications with Zend Framework.
For now, I have a huge library, wich contains each an every things, used by a couple of web applications. I am thinking of reorganising it, using the concept of "Module".
But I am not sure about the difference between Module and Package.
What I understand is :
a Module contains a part of a web application (pages, models...)
a Package is a group of class in the library
Am I seeing it right ? And how to know where should go my classes (model of a module, or library) ?
Because for example, I have some classes to do the translations. I have "model" classes, to represent a language, a text and its traductions... And I have kind of an "API" class that is just here to translate a string into a language. I would say that I need a module for the model classes and the web interface to edit the traductions, and the API class would go in the library ? Is that right ? Isn't that weird to have 2 kind of classes, one for the module and one for the library.
I guess that's an open question about API, librairie and application architecture.
Modules, in the ZF sense, group concrete, often standalone, application parts:
Modules allow a developer to group a set of related controllers into a logically organized group. The structure under the modules directory would resemble the structure under the application directory. […] The directory structure for modules should mimic that of the application/ directory in the recommended project structure
A package on the other hand is a set of classes in a code library that conceptually belong together. For instance, ActionHelpers and ControllerPlugins conceptually belong to the Zend_Controller package. All available Validator classes belong to the package Zend_Validate.
When using PHPDocumentor you can annotate your code to belong to packages. If you look at ZF's API Docs, you will see this grouping in effect. Try to find the Zend_Validate_Alpha class.
See
http://framework.zend.com/manual/en/project-structure.project.html
http://framework.zend.com/manual/en/project-structure.filesystem.html
http://framework.zend.com/apidoc/core/
http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.package.pkg.html
packages are namespace areas so that symbols with similar names do not clash with one another. For example, the symbol &main::first is different from symbol &List::Util::first . Packages are name prefixes to sysbols.
A module is a file of code, or a tree of bytecodes. A module could be precompiled (.pmc), uncompiled (.pm) on disk; or pre-loaded in memory as one unit -- assuming no autosplit.
In summary: packages are about namespaces, and modules are about files. They are different things, like apples and boxes -- until the day when you start placing one kind of apple into one kind of box, and people start thinking that apples and boxes are related. And they are! but still, one in fruit and the other is a type of container.
Simple in my view:
Modules are like smarty (a library with a targeted goal templating).
Packages are 2 or more modules that work with each other to offer a more complete solution.
Again my simple point of view.