ZEND Framework folder/file structure - php

I'm trying to learn zend framework. I managed to install it on my localhost. However i'm having trouble understanding the folder structure? There are 5 main folders after installing the skeleton application - config, data, module, public and vendor.
I've seen some proposed folder structures online, but how to I go about it? Do I just create folders like views, controllers, models etc?
Thanks!

Vendor is where composer installs dependencies and libraries, config is where configuration lives, data is for cache etc, public is where your index.php and css/js/img assets are, you are really interested in module directory that contains application modules. For the start you only really need one module - Application, inside this directory you should have config dir that has module specific config, Module.php - module bootstrap file, view with templates structured per controller and src folder with your code. Inside your src file there is your Application module namespace directory that is placed in Application directory to mimic PSR-4 autoloader namespaces it can contain your application code in this example directory structure: Controller, Form, Model. Model can contain Service, Repository and Entity folders

If you just got started with ZF2 I suggest reading some documentation. Basic things like this can all be found in the documentation. For example here you find more about the folder structure.
I would also suggest taking a look at the ZF2 Skeleton application documentation/tutorial since this will help you understand the basics of a ZF2 application. Here an example on how to structure a new module. Building the album application yourself is a really nice way to get started.

Related

Where should I put a custom class file in zend framework 3?

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/)

Directory structure in MVC PHP Frameworks

At this moment, my framework's directory structure looks like this:
framework/
libraries/
autoload/
autoload.class.php
resource.namespaces.php
router/
tests/
router.test.php
router.class.php
resource.routes.php
configuration/
framework.configuration.php
router.configuration.php
controllers/
index.controller.php
models/
index.model.php
views/
default/
index/
index.view.php
header.view.php
footer.view.php
assets/
css/
javascript/
images/
index.php
When my framework was smaller, it was a lot cleaner. I have give a look at other popular frameworks. They have two main folders:
framework/
app/
web/
Actually, this structure is very clean and nice because we separate the front-end and the back-end. But I wonder what should I put in each of these.
Logically, libraries folder should be inside the app folder, but it is not really a part of the application. For me, an application has models, controller and views. I think libraries should be put outside. Where should I put my libraries?
I has a lot of PHPUnit tests in my libraries. Should I add a folder named tests inside each library, or should I put it inside/outside the app folder?
If I want to implement a template feature, where I could choose which template I want to use for my website, how could I organise it? Every template has differents elements, so header.view.php will not be the same, etc. Now, I'm creating a folder in views which is the template folder. But I think it is a bad idea, 'cause now, for every template I must recreate all the views.
In a lot of applications, there is a vendor folder that contains all the main classes. Is this the same as my libraries folder? Does it have the same role?
I also have some 'resources' files (eg.: resources.routes.php). They are used to add some routes/namespaces. It's a bit like a configuration file. Should I create another direcotry for these files, or put them inside the class which they refers to?
I'll try to cover all your questions.
The vendors folder that you see in many apps is from composer. In that folder are all the dependencies of your application/framework (often libraries from other people).
Composer allows you to pull in some good components for your framework so that you don't have to write everything from scratch. Say for example I want to use FastRoute because it is much better than any router I am capable of writing myself. So I just add the following to my composer.json, run composer update and I can use it in my framework.
{
"require": {
"nikic/fast-route": "dev-master"
}
}
You can also use composer to autoload your classes so that you don't have to write your own autoloader (and cache that for production).
I really dislike your current file names. adding .class to a filename makes absolutely no sense to me. The dots also just complicate Autoloading. I recommend you have a look at PSR-0 for some inspiration on how to handle file names/namespaces (This is supported by the composer autoloader).
For the tests you could create a Tests folder in the root folder of your project and in there recreate the directory structure of your project and add the test classes in the matching folders. You can also put your mock objects in there. This makes it easy to just run tests for a certain part of your application.
I also recommend that you move your application out of your public folder and instead have a public folder with only an index.php and your assets. That index.php does nothing other than requiring your front controller that bootstraps your app. Doing it that way adds another layer of security if for some reason your webserver stops processing PHP. Otherwise a visitor would be able to see your whole source code.
To get some inspiration for your folder structure questions, I recommend that you have a look at how other people solved this issue. Arya and PitchBlade would be a good starting point because they are way smaller than a framework like symfony.
I hope this helps. In the future I recommend that you split up your questions into different SO questions, that makes answering easier and you will get better answers because people who know the answer to one of your questions don't also have to answer all of them.

configure zend framework library path

i want to know where can i put the "library" folder in my zend project. Presently i have it in the location of my Zend server. This is my current "library" path:
E:\zend\ZendServer\share\ZendFramework\library
And here is the path to my "app" project:
E:\zend\Apache2\htdocs\app
Inside the "app" project, i have the folders like "public" and "application".
Now how can i integrate the "library" into my "app" project, without referencing it from zendserver?
The easiest way it is to put those library files to the library dir (as in the other answers), but you should consider keeping them separated from the application.
It's easier to have separate git or svn repos for the libraries. Easy to update and maintain for multiple projects at once. Putting them in library will also won't work if you installed the libraries from the bundle, e.g. apt-get install zend-framework-library.
To have the libraries in any directory you want, just add it to the include_path array in the index.php.
Quite often, you'll see a folder called like "lib" or "libraries" in your application (next to public, application, ...).
In that folder, you'll put the frameworks you're using.
For example :
public
application
libs
Zend
Doctrine
...
To get Zend Framework, just download it, and unpack it to the right folder ;-)
Advantage : you'll be using the version of your choice -- and not the one, possibly outdated, provided by your environment.
And, as a reference, you might want to read the Installing The Zend Framework chapter of the e-book Survive the Deep end.
In your app project u should have a library folder and should contain zend folder in that..so just copy library folder and put it in E:\zend\apache2\htdocs\app.
Please don't tell people to deploy 3rd party libraries to their application project folder, it is bad practice. Deploy your libraries to a path that you have access to, which is outside of your project, and reference it in your include path.
During development, it is fine to have the library in your project, for reference sake, but exclude it in your deploy script and instead have your deploy script either set an environment variable, or have it update your include path.

How should I extend the Zend Framework as a folder organisation point of view?

I am currently integrating the Zend Framework in my current project named VMM.
I decided to put the Zend Framework directory as a standalone project next to my VMM project in Eclipse.
I need to do some customization of the zend framework (For example I need to add Irradiance.php into Measure) and I would like to know where is the best place to put all my customizations.
I know that I need to follow the Zend Framework naming convention and the same directory structure.
So for example Irradiance.php contain the Mylib_Measure_Irradiance class.
I was thinking to put the Mylib folder into ZendFramework/library/Mylib next to the Zend folder.
Is it the regular way to extend and customize the Zend Framework?
If not, should I put the customizations inside my VMM project or as an other standalone project?
Thanks!
UPDATE
This question helped me but I still need some help...
I tend not to put my own library style files into the ZF folder mainly because when you come to upgrade ZF you'll have to copy them all over to the new ZF.
On my localhost I have something like this
my-project is the project I am working on and contains all the models, views, controllers,etc for that project
/htdocs/my-project/application
/htdocs/my-project/public
library is my own library files and mimics the ZF structure
/htdocs/library/Db/
/htdocs/library/Validate/
I then have my current ZF in /usr/lib/php/ZendFramework-x.xx.x this folder contains the latest ZF and can be changed easily without changing my projects or library code base.
Edit:
David's comments about 'pointers' reminded me, I always set up a sym link in /usr/lib/php/ called ZendLatest, this points to the latest copy of ZF, this means I don't have to keep changing my code or my php.ini.
There are many ressources out there:
http://www.slideshare.net/PHPBelgium/extending-zend-framework-presentation
http://cslai.coolsilon.com/2009/03/28/extending-zend-framework/

Zend Framework: How to load existing project into ZF's CLI?

In ZF you create a project by running this command:
zf create project MyProjectName
But how does one load a project that already exists?
I don't see anything in the documentation that specifies a zf load project or zf set project or something like that.
Thanks.
This is not yet possible to my knowledge, but on the to-do list
See http://framework.zend.com/issues/browse/ZF-7940
You can simply create another zf project in a different directory and copy the xml file into the the folder where it's missing. in the xml file, everything is relative, so you don't have to worry about the newly dummy project's path and others.

Categories