0% found this document useful (0 votes)
121 views13 pages

Zend Installation Creating Controllers Creating Views Database Connectivity Naming Conventions Creating Models Rest API Calls

The document provides instructions for setting up a basic Zend Framework application including installing Zend Framework, creating controllers, views, database connectivity, and models. It discusses naming conventions and outlines the basic steps to create controllers, views, connect to a database, define models by extending the Zend_Db_Table_Abstract class, and other Zend Framework conventions and best practices.

Uploaded by

pass123word
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views13 pages

Zend Installation Creating Controllers Creating Views Database Connectivity Naming Conventions Creating Models Rest API Calls

The document provides instructions for setting up a basic Zend Framework application including installing Zend Framework, creating controllers, views, database connectivity, and models. It discusses naming conventions and outlines the basic steps to create controllers, views, connect to a database, define models by extending the Zend_Db_Table_Abstract class, and other Zend Framework conventions and best practices.

Uploaded by

pass123word
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Zend Installation Creating Controllers Creating Views Database Connectivity Naming Conventions Creating Models Rest API Calls

Zend Installation: 1. Go to URL : http://framework.zend.com/download/latest 2. Download any one Zend Framework 1.11.11 Full. Zend Framework 1.11.11 Minimal. 3. Extract the contents; make a note of where you have done so. 4. You should be able to run php.exe on the command line. (Adding to the system's environment variable). 5. zf.bat (in the /bin/ of the extracted folder) is reachable from your system path. This is the ability to call zf from anywhere on your command line, regardless of what your current working directory is (Adding to the system's environment variable). 6. Add the ZendFramework/library (in the extracted path) is in your include_path. 7. Open a terminal (in Windows, Start -> Run, and then use cmd). Navigate to a directory where you would like to start a project. Then, use the path to the appropriate script, and execute one of the following: % zf create project <<projectName>>

Running this command will create your basic site structure, including your initial controllers and views. The tree looks like the following: 8. Copy the contents of the ZendFramework/library (from extracted path) to the newly created project's /library folder. 9. Your Bootstrap class defines what resources and components to initialize. By default, Zend Framework's Front Controller is initialized, and it uses the application/controllers/ as the default directory in which to look for action controllers . 10. Zend Framework is itself configurationless, you often need to configure your application. The default configuration is placed in application/configs/application.ini, and contains some basic directives for setting your PHP environment (for instance, turning error reporting on and off), indicating the path to your bootstrap class (as well as its class name), and the path to your action controllers. 11.Create the virtual host document root points to the folder named public inside that project.

Creating Controllers When building a website based on the Model-View-Controller (MVC) pattern. To create a controller create a class that extends Zend_Controller_Action, with the action methods that correspond to the various actions you wish the controller to handle for your site. All action method names should end with the word Action. For example, let's say your class is defined as follows:
<?php class UserController extends Zend_Controller_Action { public function init() { } public function indexAction() { $user = new Application_Model_User(); $this->view->users = $user->fetchAll(); $this->view->headings = $user->info('cols'); $this->view->message = "WELCOME"; } public function createAction() { $this->view->message = "Create User"; } }

By default all the controller files are searched inside the controller folder. Save the controller file inside the folder application\controllers. The more appropriate way to customize instantiation is to use the init() method, which is called as the last task of __construct(). To assign the values to the view file variables just use the syntex as follows. $this->view-><<VariableName>> = <<Value>> This variable can be accessed inside the view file as $this-><<variableNAme>> View files will be automatically rendered at the end of the each method (action methods).

Creating Views View files should be saved in the name of action method names with the extension .phtml e.g. For the above UserController ------------------------------------------------------------------------------------------------------------------Action Method View File Name ------------------------------------------------------------------------------------------------------------------indexAction index.phtml. createAction create.phtml All view scripts (files) are stored inside the folder /views/scripts/ <<controllerName>>/ E.g. In UserController For each action there will be corresponding views files inside the directory '/views/scripts/user/' --------------------------------------------------------------------------------------------------------------------------Action Method View File Name Location Saved --------------------------------------------------------------------------------------------------------------------------indexAction index.phtml. /views/scripts/user/index.phtml CreateAction create.phtml /views/scripts/user/create.phtml --------------------------------------------------------------------------------------------------------------------------Note : If your action method contains camelCasing, please remember that this will result in '-' separated words when determining the view script file name. Example view file. Index.phtml.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Zend Framework Default Application</title> </head> <body> <h2><?php echo $this->message ?></h2> <pre> <?php print_r($this->headings); while($user = $this->users->current()) { echo "<br>USER NAME : " . $user->name ."<br>"; print_r($user->toArray());

$addresses = $user->findDependentRowset('Application_Model_AddressDetails');

while($address = $addresses->current()) { print_r($address->toArray()); $addresses->next(); } echo "<br>"; $this->users->next(); } ?> </pre> </body> </html>

Database Connectivity : For Database configurations add update the bootstrap.php file as shown below.
<?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { public function _initDb() { $options = array( 'host' => 'localhost', 'username' => 'root', 'password' => 'root', 'dbname' => 'zendsample' ); $db = Zend_Db::factory('PDO_MYSQL', $options); Zend_Db_Table_Abstract::setDefaultAdapter($db); } }

There are more ways for creating a database connectivity, Refer : http://framework.zend.com/manual/1.11/en/zend.db.adapter.html

Naming Conventions Classes : Zend Framework standardizes on a class naming convention whereby the names of the classes directly map to the directories in which they are stored. Class names may only contain alphanumeric characters. Numbers are permitted in class names but are discouraged in most cases. Underscores are only permitted in place of the path separator; the filename "Zend/Db/Table.php" must map to the class name "Zend_Db_Table". Filenames : For all other files, only alphanumeric characters, underscores, and the dash character ("-") are permitted. Spaces are strictly prohibited. Any file that contains PHP code should end with the extension ".php", with the notable exception of view scripts. The following examples show acceptable filenames for Zend Framework classes: Zend/Db.php Zend/Controller/Front.php Zend/View/Helper/FormRadio.php File names must map to class names as described above. Functions and Methods : Function names may only contain alphanumeric characters. Underscores are not permitted. Numbers are permitted in function names but are discouraged in most cases. Function names must always start with a lowercase letter. When a function name consists of more than one word, the first letter of each new word must be capitalized. This is commonly called "camelCase" formatting. Verbosity is generally encouraged. Function names should be as verbose as is practical to fully describe their purpose and behavior. These are examples of acceptable names for functions: filterInput() getElementById() widgetFactory() For object-oriented programming, accessors for instance or static variables should always be prefixed with "get" or "set". In implementing design patterns, such as the singleton or factory patterns, the name of the method should contain the pattern name where practical to more thoroughly describe behavior.

For methods on objects that are declared with the "private" or "protected" modifier, the first character of the method name must be an underscore. This is the only acceptable application of an underscore in a method name. Methods declared "public" should never contain an underscore. Functions in the global scope (a.k.a "floating functions") are permitted but discouraged in most cases. Consider wrapping these functions in a static class. Variables : Variable names may only contain alphanumeric characters. Underscores are not permitted. Numbers are permitted in variable names but are discouraged in most cases. For instance variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore. This is the only acceptable application of an underscore in a variable name. Member variables declared "public" should never start with an underscore. As with function names (see section 3.3) variable names must always start with a lowercase letter and follow the "camelCaps" capitalization convention. Verbosity is generally encouraged. Variables should always be as verbose as practical to describe the data that the developer intends to store in them. Terse variable names such as "$i" and "$n" are discouraged for all but the smallest loop contexts. If a loop contains more than 20 lines of code, the index variables should have more descriptive names. Constants : Constants may contain both alphanumeric characters and underscores. Numbers are permitted in constant names. All letters used in a constant name must be capitalized, while all words in a constant name must be separated by underscore characters. For example, EMBED_SUPPRESS_EMBED_EXCEPTION is permitted but EMBED_SUPPRESSEMBEDEXCEPTION is not. Constants must be defined as class members with the "const" modifier. Defining constants in the global scope with the "define" function is permitted but strongly discouraged.

Creating Models Defining a Table Class : For each table in your database that you want to access, define a class that extends Zend_Db_Table_Abstract. $_name : Declare the database table for which this class is defined, using the protected variable $_name. This is a string, and must contain the name of the table spelled as it appears in the database. If you don't specify the table name, it defaults to the name of the class. If you rely on this default, the class name must match the spelling of the table name as it appears in the database. $_sequence : In Zend_Db_Table_Abstract, if you define the protected variable $_sequence to be the Boolean value true, then the class assumes that the table has an auto-incrementing primary key. File User.php Saved under application\models\User.php
<?php class Application_Model_User extends Zend_Db_Table_Abstract { protected $_name = 'user';
protected $_dependentTables = array('Application_Model_AddressDetails');

File AddressDetails.php Saved under application\models\AddressDetails.php


<?php class Application_Model_AddressDetails extends Zend_Db_Table_Abstract { protected $_name = 'address_details'; protected $_dependentTables = array ('Application_Model_User'); protected $_referenceMap = array ( Application_Model_AddressDetails' => array( 'columns' => array('user_id'), 'refTableClass' => 'Application_Model_User', 'refColumns' => array('id') ) ); }

For More details refer URL : http://files.zend.com/help/Zend-Framework/zend.db.table.html#zend.db.table.introduction

Rest API Calls You can simply call the REST service procedures as Zend_Rest_Client methods. Specify the service's full address in the Zend_Rest_Client constructor. Request Arguments Sending multiple arguments with your request is done by calling a method with the name of the argument, passing in the value as the first (and only) argument.

<?php class UserController extends Zend_Controller_Action { public function restAction() { $client = new Zend_Rest_Client('http://localhost/test/rest.php'); $client->name('Balaji'); $client->age('25'); $result = $client->post(); //post() is the method how you want send data.

$this->view->name = $result->name(); $this->view->age = $result->age(); } }

Zend Creating HTML Forms Zend_Form simplifies form creation and handling in your web application. It performs the following tasks: Element input filtering and validation Element ordering Element and Form rendering, including escaping Element and form grouping Element and form-level configuration Zend_Form makes use of several Zend Framework components to accomplish its goals, including Zend_Config, Zend_Validate, Zend_Filter, Zend_Loader_PluginLoader, and optionally Zend_View.

You might also like