Reputation: 105
I'm new to MVC and ZF2.
I carried out their tutorial successfully to build a basic app. (http://framework.zend.com/manual/2.1/en/user-guide/overview.html). In that tutorial they create a module called 'Album' to add/edit/delete music albums.
What I want to do is create an application that will have various functions, such as user account administration, system config, etc. I'm aware via reading other posts that you don't need to create a new module for each function, rather grouping them under one using entities.
With that in mind I set out creating my first module 'User' using this structure but I'm not sure if I'm doing it right. Structure I have at the moment is:
/module
/Application
/config
/language
/view
/src
/Application
/Controller
/UserController.php
/Entity
/User.php
/UserTable.php
/view
/user
/user
/index
If you want a look at the code check out the repo at https://bitbucket.org/newvisionjames/zf2-test/overview
Specific questions I have are:
1) I have two 'view' directories at the moment, pretty sure that's wrong..which one is correct, if either?
2) In the ZF2 tutorial they create two php files under /model
called Album.php
and AlbumTable.php
. I have reflected this is my /Entity
folder. Is this direct transfer correct? Does having an /Entity
directory render the /model
not necessary?
Overall what I'm trying to do is get set up with this framework so that it's working and I'll be able to learn from there but right now I'm stuck! Any helpful answers or pointers to useful resources will be much appreciated.
Thanks.
Upvotes: 0
Views: 738
Reputation: 2497
You should definitely read this post: http://akrabat.com/zend-framework-2/thoughts-on-module-directory-structure/
Upvotes: 0
Reputation: 1701
Well-written and nicely formatted question. Don't see that too often.
src
. Everything under src will be class files. So they follow the PSR-0 standard, each file contains excatly one class, etc. So you don't want any views there.model
directory and that will probably put all of your entities inside Module\Entity
. So that's absolutely right.Overall I'd tell you this: Stick with the config
, src
, view
folders. That's what ZF2 likes and this is what every other ZF2 developer will expect from a ZF2 module. Don't get too hung up on conventions inside your src
folder though. You can pretty much put your stuff whereever you want in there. If you've got a decent IDE (like PhpStorm), it only takes a couple of seconds to move or rename classes later. Just go through some ZF2 modules on GitHub and try to match what groupings or hierarchies you see most often.
And here are some words about the separation of modules: I'd say it's a good practice to try and break your project up into modules as much as possible, as long as you think they could live separately. For example: You have an online shop with a bunch of products and a search function. Now ask yourself the following question: "Do the products need the search function?" No. The products can live without it. So you put the search functionality in its own ProductSearch
module. Can blog posts live without their comments? Yes. Create a Blog
and a BlogComment
module.
The whole project structure is something I was thinking a lot about when I started out with ZF2. So if you have any more questions, just comment and I'll try to update my answer.
Upvotes: 1
Reputation: 1448
ZF2 follows PSR-0 standard which also improves the ability for other framework modules to interact with ZF2. To learn more about PSR-0 standard, take a look here: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
With your example above mentioning user, you most likely would keep user in it's own module due to the nature of "user". Does the user just contain a class to represent the user or does it include hooks for authorization and authentication etc? Also how is the routes handled? Although user may seem simple at first glance it's usually something that is placed in it's own module.
And that is so you can re-use / share your module with others. Generally whenever I build an app it would be moved into multiple modules depending on where the functionality fits. Then between modules if I need access to another module I will make use of the service locator to access those methods.
For instance under the application module I may want to reference the user's first name, i can call the user module service to then show me the user's details. This simplifies your code a lot.
To answer your questions directly though:
Answer: Based on your posted directory structure only the top level "view" directory looks correct in a "ZFSekeleton" representation.
Answer: Technically you can put your business logic anywhere as long as it follows namespace / psr-0 standards. Beyond that there is no real requirements, although what is important to you use a coder is that you improve readability of your code or you won't get anywhere fast.
I normally like to seperate my src folders like so:
Controllers Models Adapters Services Entities
Where controllers is obviously the controllers for zf2. Models contains some business logic for controllers or classes. Adapters are authentication adapters, database adapters etc. Services are the service locator classes for other modules and the current module to work from. Entities are my data representations.
To each their own but that just makes sense to me. Hope that helps.
Upvotes: 1