I have a zend framework project, all written in one Module. Under this Module you find a controller directory, Model directory and view directory. I am looking for a perspective to devide this Module into many, each with its controllers, models and views. would you supply me with some references to read more about this.
Thanks in advance.
You should not divide your module, but introduce several modules. Each module has its own config, src and view folder. Inside the src folder is another folder (same name as Module) and in this src folder you find controller, view and model folders (and more depending on the project).
So something like this:
As far as I know this is the correct layout in a ZF2 application.
You can also read on folder structure in the ZF2 documentation.
And on ZF2 project structure here
And on setting up a module here in the ZF2 user guide
Related
I am developing REST API using Phalcon. The application will have multiple modules like Users, Company, Contacts and so on. Each of these modules have their own tables for storing data. Moreover each module have their own defination file which mentions what fields to show in API response. I am pretty new to Phalcon, i just started learning Phalcon, and I need help regarding how i should structure the application so that this code won't give me future problems, or if I'm missing something or if the code could be abstracted more then that would be great.
Directory Structure as planned:
app/
MyAPI/
MyAPIControler.php
library/
controller.php //master controller where all controllers inherit from
model.php //master model where all models inherit from
utilities.php
MyAPI/
models/
User.php
Contacts.php
Company.php
Myapi.php
/config
config.php
routes.php
index.php
I want all database related queries of each module to reside in their own models. API URL say for listing of records will be
http://api.example.com/MyAPI/V2/contacts/list or /MyAPI/V2/users/list. Similarly API URL for creating records will be http://api.example.com/MyAPI/V2/contacts/add or /MyAPI/V2/users/list
Please advise how i should proceed
The project folder structure would be better if it's something like this,
app/
config/
controllers/
forms/
library/
models/
views/
cache/
public/
css/
img/
schemas/
The folder names are self explanatory. Since your trying to make a REST API, you can remove the public folder along with css and img in it. You can also set the default render to be json in the base controller and have the other controller's extend it.
I've started using Zend framework, and Im following this simple Zend form tutorial. The form is located in application/forms directory I have included the Zend framework in the Path (by going to computer properties). However when i access form.php page, i get this error:
Fatal error: Class 'Zend_Form' not found
I have also tried by copying the Zend folder from the Zend framework in the library folder of the application, however error still remains same. Thanks.
It sounds like you are confusing two notions: the system path and the include path.
The system path is an operating system concept. When you ask the OS to execute a command, the system path is a list of places to look for the executable.
In contrast, the include path is a PHP concept that tells PHP a list of folders in which to look for files invoked by PHP include/require statements.
Any path that you find in "Computer > Properties" is almost certainly the OS-level system path. What you need to do is to make sure the Zend folder on the PHP include path, either by moving the Zend folder or by modifying PHP's include path to include a point to the Zend folder.
In a typical ZF app, the include folder is set in index.php (the front controller). So, the only explanation for not finding Zend_Form is that the autoloader is not being instantiated.
If you are using Zend_Application, this happens automatically. However, it seems like you are bypassing public/index.php and the whole Zend_Application instantiation by trying to directly "access" a file called form.php directly. If this file contains only the definition of your form (extending Zend_Form), then the absence of autoloading could easily explain the error message you are getting.
I'd try instantiating the form in a controller action, by which time in the request processing cycle, the autoloading is probably already in place.
[At that point, given the file locations you cite, we might run into a resource-loader issue, but that's a somewhat different issue that can be handled by instantiating a Zend_Application_Module_Autoloader in your Bootstrap.]
It looks like you do not use Zend_Loader. You should focus on it.
You can also manually in your custom form class include Zend_Form class.
Usually i create controller and rest of the work (actions method) and view files i do manually. does .zfproject.xml file has any significance?
zfproject.xml is used by Zend_Tool to store information about created controllers, actions, etc. within project - but only these created by using zf command.
I am trying to distribute a netbeans project however the jar it creates and the contents of the dist folder are dependant on some image files which i included into the project - however these images are not in the dist folder and I cannot workout how to make things work so I can export the project in a distributable format including all the things it needs.
Can somebody please tell me how I can export a project which runs within Netbeans without using the project's /dist folder which includes everything it needs?
Cheers
Andy
One way to achieve this is to add a folder (f.i."resources") in your project's src dir. Then copy the images to that dir. Now the images should get included when you build the project (if I remember correctly). Accessing the files can be accomplished with "getResourceAsStream"...
If whatever resources you are interested in are in the classpath, packaged in the jar, war, or the distribution, you can retrieve them by getting resources.
The convention is indeed to have a directory named 'src/resources' that serves as the root for this. Depending on the amount and scope of the resources you are using you may also want to add a sub-directory hierarchy to keep the organization and state of the resources manageable.
Also, not that a resource can be any file, an image, sound, text, xml, binary, etc. no limitation.
Finally, the call will look like this if you are using an object method:
getClass().getResourceAsStream("resources/myResource") - or - getClass().getResource("resources/myResource")
depends on if you want a stream or just the URI at that point in the code. Typically one would use the URI for delegating the processing of the resource elsewhere and the stream form when you are processing it in-line.
For a class method, you will need to do something more like:
new Object().getClass()...
The think to keep in mind here, is eventually this is resolving to the class loader and it is from that class path that the resource will be fetched.
You can add images the same way:
final Image image0 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/1.png"));
I don't have permission to change the document root the /public/ directory so how should I set up my Zend Framework application to run from the current root directory? Using the Zend Framework 1.8 command line tool, I don't know if there is a way to tell it to create a directory structure this way.
If you can access only the upper level of web (i.e. - public), you should set index there and the whole application folder too. Create a .htaccess with
Deny from all
And put it into your /application.
Your configuration will be:
/application
/library
index.php
The simplest way without changing a lot of configuration, is to put everything in the public folder you mention into your public_html folder, then place all the other contents, like the application, and library folders into the directory up from public_html.
You can also throw everything into your public_html folder, although that is not recommended. Each class has options to provide a different path. For example on the Front_Controller, you can set the Controllers directory to wherever you want. There are options to specify different paths, but if you follow convention it is done for you.
Just use the quickstart guide and adjust according to it. Zend_Tool is still experimental anyway. Let me know if this helps.
So here's what I ended up doing:
Download the Quickstart sample code.
Move everything in public up to the main directory, along side application, library directories.
Alter include paths to library and application in index.php to point to the correct locations
I think that was all I had to do. ZF new how to the rest.
I don't think this is ideal however, as already mentioned, application directory becomes accessible from the web, but for now, it's getting the job done.