Zend Framework Tutorial: Following another Quickstart

I am following another quickstart tutorial for Zend Framework written by Rob Allen. This tutorial is far better than the tutorial on official zend framework site. I am also little experienced after following super tutorial on Zend Framework so it is gonna easier anyway.

I thought to write all commands I am going to use for this tutorial and I will see the number of commands required to complete this. I am still following this tutorial but here are the few commands I have gathered till now!

Steps and Commands:

1. zf create project quickstart2

2. SetEnv APPLICATION_ENV development
to .htaccess file

3. Added phpSettings.date.timezone = "Asia/Calcutta"
in Development section ([development : production]).

4. zf create action add Index for adding action 'addAction' in Index Controller.
zf create action edit Index for adding action 'editAction'
zf create action delete Index for adding action 'deleteAction'

5. Add this to application.ini file
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 123456
resources.db.params.dbname = quickstart2

6.
Run this query from Mysql client (phpmyadmin etc)
CREATE TABLE albums (
id int(11) NOT NULL auto_increment,
artist varchar(100) NOT NULL,
title varchar(100) NOT NULL,
PRIMARY KEY (id)
);

Need to create DB first. Db name mentioned above in application.ini is quickstart2

7. Populate the table with below query.

INSERT INTO albums (artist, title)
VALUES
('Paolo Nutine', 'Sunny Side Up'),
('Florence + The Machine', 'Lungs'),
('Massive Attack', 'Heligoland'),
('Andre Rieu', 'Forever Vienna'),
('Sade', 'Soldier of Love');

8. zf create db-table Albums albums
Albums is class name and albums is table name it maps to.

9. Writing codes to newly created file Albums.php file for adding, deleting,...

For selecting row:


	public function getAlbum($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Could not find row $id");
		}
		return $row->toArray();
	}

For Adding:


	public function addAlbum($artist, $title)
	{
		$data = array(
			'artist' => $artist,
			'title' => $title,
			);
		$this->insert($data);
	}

For updating:


	public function updateAlbum($id, $artist, $title)
	{
		$data = array(
		'artist' => $artist,
		'title' => $title,
		);
		$this->update($data, 'id = '. (int)$id);
	}

For deleting row:


	public function deleteAlbum($id)
	{
		$this->delete('id =' . (int)$id);
	}

10. Now, we need to fill in the controllers with the data from the models and get the view scripts to display it.

First Layout creation for use in each View:
zf enable layout

11. Now need to replace very basic layout code with our needed layout code in layout.phtml.

12. resources.view.doctype = "XHTML1_STRICT"
to the application.ini (won't it work without it!)

13. We use a view helper function to add css file to layout.phtml.
$this->baseUrl() gives use baseURL.
<?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/site.css'); ?>

12. Now, we will fill the views - views/scripts/Index/add.phtml, edit.phtml, ..
and simultaneously fill the controller for each view.

IndexController.php: method indexAction()

$albums = new Application_Model_DbTable_Albums();
$this->view->albums = $albums->fetchAll();

Filled views/scripts/index/index.phtml. Now, the above controller action will use
the index.phtml view.
13. Need a form to add album. So,
zf create form Album
The above command created Application/forms/Album.php. Added code to init().

14. Now form is added. So, we need to take care of its display from controller and add the code for form submission.
So, modified indexController.php addAction() method.

15. After the above command, I do not issue any single command to develop a fully functional sample
application. I need to add (yes, not write as I am copying the code from the source mentioned) codes
to the controller and view files.

Good point about view of add album is that code for viewing the page is very simple.


	<?php
		$this->title = "Add new album";
		$this->headTitle($this->title);
		echo $this->form ;
	?>
	

So, add album is complete after adding code for controller addAlbum() and adding code to view add.phtml.

16. Similarly, I need to add codes on edit and delete controllers and views.

- - - - - - - - - - -


- - - - - - - - - - -

Here are few points mentioned there:

Points:
1.
Supporting images, JavaScript and CSS files are stored under the public/.
The downloaded Zend Framework files will be placed in the library/ directory. If we need to use any other
libraries, they can also be placed here.

2.
Copy the library/Zend/ directory from downloaded archive file (ZendFramework-1.10.6-
minimal.zip) into your zf-tutorial/library/, so that your zf-tutorial/library/ contains a
sub-directory called Zend/.

I do not need this as I have included the Zend files in include_path.

3.
The, application.ini is loaded using the
Zend_Config_Ini component. Zend_Config_Ini understands the concept of inheritance of sections
which are denoted by using a colon on the section name. For example:
[staging : production]

3.

http://localhost/public/zf-tutorial/news/view
Here 'news' is controller and view is Action.
A controller can have many actions - News/list, news/archived, etc.

Zend framework has defualt controller called 'index' and default action also called 'index'.
so http://localhost/ZendFramework/demos/quickstart2/public/ works.

Controllers can be grouped into modules.

4.
{Controller name}Controller is the form controller takes. Controller name ({Controller name}) starts with Cap letter and other letters as small letter.
PHP file name will be {Controller name}Controller.php.
Actions within controller are named like this: {action name}Action. {action name} starts with small letter. All other letters are also lower case.

5.
zf create action add Index
Adds action names 'add' in Index Controller.

6.
If you get 404 error on
http://localhost/ZendFramework/demos/quickstart2/public/Index/edit
after creating edit Action in Index controller then you may have not enable mod_rewrite or
you may have not changed the AllowOverride so that .htaccess can be used.

7.
With each new action created using Zend tool, new view files are automatically created.
So, latter we need to create our models. In model comes DB related job and business rules.
For DB jobs of find, insert, update, and delete rows, ZF Class Zend_Db_Table is used.

8.
Zend_Db_Table which is used to find, insert, update and delete rows.

9.
At the end of the dispatch cycle, after the controller action methods have finished, Zend_Layout will render
our layout.

10. There is a variable, $this, available in layout.phtml, which is an instance of the view object that was
created during bootstrapping.

CONTINUE!

Both points and commands list will grow as I follow the article.

My following of the above mentioned tutorial about Zend Framework quickstart tutorial is over.

Comments are open for an year period. Please, write here on Facebook page.