Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using forms with the Zend Framework

I'm relatively new to MVC and the Zend Framework. That being said, I feel like I have a hard time figuring out where Forms belong in my directory structure. I have a modular directory structure, so I don't know if there should be a single forms directory, or one within each module directory.

/application
    /modules/
        /default
            /controllers
            /views
        /admin
            /controllers
            /views

Once you've decided a directory for forms, do you set that directory in the include path of the bootstrap? Or do you include the form in the controller that it's being used in?

How do you use forms with the Zend Framework?

like image 526
Andrew Avatar asked Mar 23 '09 18:03

Andrew


People also ask

How do I submit a form in Zend Framework?

use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; use Album\Model\Album; // <-- Add this import use Album\Form\AlbumForm; // <-- Add this import //... // Add content to this method: public function addAction() { $form = new AlbumForm(); $form->get('submit')->setValue('Add'); $request = $ ...

Is Zend Framework Good?

When it comes to PHP frameworks, Zend is counted among the best. Zend Framework offers lots of benefits for creating feature-rich and dynamic web solutions. MVC features and a strong component library have made Zend a popular PHP framework for creating a myriad of web solutions.

What is the use of Zend Framework?

Zend Framework is a collection of professional PHP packages with more than 570 million installations. It can be used to develop web applications and services using PHP 5.6+, and provides 100% object-oriented code using a broad spectrum of language features.

Is Zend Framework an MVC?

Zend\Mvc is a brand new MVC implementation designed from the ground up for Zend Framework 2, focusing on performance and flexibility. The MVC layer is built on top of the following components: Zend\ServiceManager - Zend Framework provides a set of default service definitions set up at Zend\Mvc\Service.


2 Answers

It's a little late but in the current version of ZF this has been solved:

On the following page http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html The manual states:

30.3.2. The Module Resource Autoloader Zend Framework ships with a concrete implementation of Zend_Loader_Autoloader_Resource that contains resource type mappings that cover the default recommended directory structure for Zend Framework MVC applications. This loader, Zend_Application_Module_Autoloader, comes with the following mappings:

api/         => Api
forms/       => Form
models/      => Model
    DbTable/ => Model_DbTable
plugins/     => Plugin

As an example, if you have a module with the prefix of "Blog_", and attempted to instantiate the class "Blog_Form_Entry", it would look in the resource directory's "forms/" subdirectory for a file named "Entry.php". When using module bootstraps with Zend_Application, an instance of Zend_Application_Module_Autoloader will be created by default for each discrete module, allowing you to autoload module resources.

This does, however, require the use of Zend_Application

like image 162
NDM Avatar answered Oct 17 '22 19:10

NDM


add this in application/modules/yourmodule/Bootstrap.php file.

class Yourmodule_Bootstrap extends Zend_Application_Module_Bootstrap
{

protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Yourmodule_',
            'basePath'  => APPLICATION_PATH .'/modules/yourmodule',
            'resourceTypes' => array (
                'form' => array(
                    'path' => 'forms',
                    'namespace' => 'Form',
                ),
                'model' => array(
                    'path' => 'models',
                    'namespace' => 'Model',
                ),
            )
        ));
        return $autoloader;
    }

}
like image 31
abulbul Avatar answered Oct 17 '22 19:10

abulbul