Homepage
I, Me and Myself
Portfolio
Mindfolio
Blog
Guestbook

Blog > Zend Framework

Post 9: Managing application.ini and session data in Zend Framework

Posted on February 09, 12 @ 07:05 am under Zend Framework and has no comments.

A. Accessing application.ini data
There will be times when configuration items are needed in the logic. There is a way you can access it.

This line can be given in init() of the controller.
$this->_config = $this->getInvokeArg('bootstrap')->getOptions();


Now the config data is available as array for controllers.
_____________________________________________________________________________________


B. Accessing session data.
Session data is available as class members by instantiating session namespace.

For easiness,
1. the session namespace can be specified in the application.ini. and can be called in the code.
2. as an alternate, instead of calling many times, in init(), you can instantiate a class member variable, which will be available for the controller.

$this->_session = new Zend_Session_Namespace($this->_config['sessionName']);


_____________________________________________________________________________________

C. Making session data available for view.
In init() of controller, you can add following line, after the above line.
$this->view->sessionData=$this->_session;

Post 8: Enabling Modules in Zend Framework

Posted on February 09, 12 @ 06:23 am under Zend Framework and has no comments.

Modules will help organizing the program logic into folders and files. Modules can be enabled as follows.

Using zf command tool, give this command in terminal: zf create module MyModuleName

This will create modules/MyModuleName directory.

To create controllers in this module, you need to specify the module name in zf create controller command.

Example: zf create controller Dashboard index-action-included=1 MyModuleName

Note: The index-action-included=1 option will automatically create indexAction() in the controller created.

If the module name is not specified, the controller will be created as the normal controller.

Post 7: Using custom libraries in Zend Framework

Posted on February 09, 12 @ 06:08 am under Zend Framework and has no comments.

While developing projects based on Zend Framework, programmers usually consolidate some classes and methods, which they want to reuse in other projects. For example, displaying datagrids, custom calendars, etc.

In that case, those libraries can be present in library folder. as per the predefined structure.

For example. Jasmine_Db_Datagrid class should be present in zf_project/library/Jasmine/Db/Datagrid.php.

i.e while auto loading ZF will replace underscore with slash and includes that file.

Wait Wait !!!

Having the files is not enough to auto enable it, They have to be configured in the application.ini file, with these two lines.

autoloadernamespaces[] = "Zend"
autoloadernamespaces[] = "Jasmine"

Note: The Zend namespace should also be included.

Post 6: Adding database information in Zend Framework

Posted on February 09, 12 @ 05:45 am under Zend Framework and has no comments.

In application/configs/application.ini, there will be production, testing and development sections. Settings given in production section can be overridden in others.

Following lines have to be added, for MySQL database. The values can be changed for sqlite, or odbc or some other, according to the configuration planned.

; database settings
resources.db.adapter="pdo_mysql"
resources.db.params.dbname="my_app_db"
resources.db.params.username="my_app_db_user"
resources.db.params.password="my_app_db_user"
resources.db.params.host="localhost"
resources.db.isDefaultTableAdapter = true

Few tutorials have said to add it Zend_Registry and all. But its not needed, adding just here, makes the database available to the models developed.

Post 5-2: Organizing images, styles, javascripts in Zend Framework

Posted on February 09, 12 @ 01:56 am under Zend Framework and has no comments.

Images, stylesheets, javascripts can be placed in public folder and their references can be given using $this->baseUrl(); in the view scripts.

For example, to include an image, in the view script (.phtml file of controller's action):
< img src="< ?php echo $this->baseUrl(); ? >" ... />

Similar code can be used for Javascript files, styles etc.
< script language="javascript" src="< ?php echo $this->baseUrl(); ? >">< /script>

The baseUrl() value can also be accessed from controller by using this:
$this->view->baseUrl();

This technique is useful in layouts, where the layout file has to include these assets.

For ease of use, at the beginning of layout.phtml, you can specify
< ?php $viewBaseUrl=$this->baseUrl(); ? >

This will be useful in reducing the calls to the baseUrl() methods and improves performance.

Post 5: Zend Layout

Posted on September 26, 11 @ 08:35 pm under Zend Framework and has no comments.

In ZF, enabling Zend layout can be done in 2 ways,

A. Using zf batch script
For this, change dir to the project directory and give this command in terminal or command line.
> zf enable layout

B. Manual process.
This has 4 steps.
1. specifying layout path in Bootstrap.php
2. Creating a default layout file in the layout path
3. Specifying the content areas for data
4. Using custom layouts

1. Specifying layout path
Create a folder named layouts in application/views/, or where ever the layout files are planned to be placed. After that in application/Bootstrap.php, specify the layout path in startMvc method of Zend_Layout in run() method. (runApp() in older ZF versions).

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
public function run() {
//..... other code
Zend_Layout::startMvc(array('layoutpath'=>APPLICATION_PATH.'/views/layouts'));
//... other code
parent::run();
}
}

2. Creating a default layout file in the layout path
Create a file named layout.phtml file in the application/views/layouts folder. This acts as template for the Zend site. This is the default layout file picked up, when no specific layout is asked for. This file contains the template html code, where the content html generated from view scripts gets included.

3. Specifying the content areas for data
In the layout.phtml, the content area is specified by the following code

echo $this->layout()->content;

The content area will have the html code generated by the view scripts.

Including another .phtml content inturn into layout.phtml can be done by the partial() method. Suppose a menu html code in _menu.html is to be included into the layout, the code will be,

< p> Site Menu: < ?php echo $this->partial('_menu.phtml'); ? >< /p>

4. Using custom layouts
In the layouts folder, you can create your own layout with the extension phtml. e.g. admin-home-layout.phtml. This custom layout should be specified in the desired controller.

$this->_helper->_layout->setLayout('super-admin-home-layout');

After specifying this, the variables between controller and view can be passed normally.

Post 4: Understanding zend framework folder structure

Posted on April 06, 11 @ 12:41 pm under Zend Framework and has no comments.

When you create a project with zf command line tool you will have these folders

application: All your application models, views and controllers will reside in application folder.
docs: Will have your documentation
library: will have your library scripts or any 3rd party scripts
public: has index.php, which acts as entry point into your application.
tests: will have your application test scripts.

With in application folder, you will have

configs: contains configuration files, these will, by default, have ini extension.
controllers, models, views: will have respective files.
and Bootstrap.php

How the scripts are accessed from browser?
The url will be of following pattern: http://website.com/controller/action/parameters

Where to place what?
The final output you see on browser will have a controller, having business logic and view, having the html content. If you use any datasource, such as database, you will also have models. you need to place those scripts individually in these folders.

Post 3: Accessing Zend library on shared hosts

Posted on April 06, 11 @ 11:53 am under Zend Framework and has no comments.

Remember the library folder I told in Post 1? If you have access to php.ini folder, you will add the path in the include_path. What about shared webhosts, especially godaddy.com, where you dont have access to php.ini.

I have my second trick for that here. Just copy the complete Zend folder into library folder.

If you get Warning: require_once(Zend/Application.php): failed to open stream: No such file or directory in error in so-so folder, check the permissions on zend library folder. If the apache user is not able to access the files, it will show this error. I had struggled for almost one hour to trace this out, and once i chmod-ded it, it got included.

Now if you access the application again, your welcome page will show up.

Post 2: Creating application with zend framework

Posted on April 06, 11 @ 10:03 am under Zend Framework and has no comments.

I assume that you have two work areas to check the application

1. A development server, or your desktop, where you have limited public access but complete control on your webserver internals.
2. A live server, or production server, where you will deploy your application for public use.

When you work in a professional environment, you might have many servers in between, like UAT server, testing server, etc etc.

Deployment at a glance
Dont get confused when I jump out and talk deployment, even before creating the application. You just need to understand how the application will be accessed.

1. On development server: On apache you can add a virtual host and point it to your application folder, and configure the hosts file to access it.
2. On live server: You already have your webroot configured, so you can just copy paste your application contents into that.

Creating a virtual host in Apache:
Google it and configure according to your environment. Point that virtual host to a empty directory, where you want your application to reside.

Many programmers say that the virtual host should be configured after creating the zend project, so that it points to public folder within that. That is true on a server on which we have a control. But shared webhost provider will not allow you to specify the folder of your choice. You have to adjust in what he gives. I have a trick for that. Will tell two tricks for that.

Creating zend application from zf command line tool
Give the command >zf create project.
It will ask for a path, give the path of the newly created virtual host folder. Few folders and files will be created. But you will not see any index.php as a starting point. Actually that index.php is in public folder again. So just create another index.php in your main folder and include public/index.php. i.e. include('public/index.php'); This is my first trick.

Now access your virtual host from browser. You will see a Welcome page. if you dont see a welcome page, check out the steps that you did previously or search on Google.

Note 1:
On ubuntu, either virtual host can be configured, or an alias can be created till the public folder. There after in public/.htaccess, add this line: RewriteBase /

Note 2:
On ubuntu, alias can be configured in /etc/apache2/sites-enabled/00-default.

Post 1: Intro and configuring Zend Framework

Posted on April 06, 11 @ 08:17 am under Zend Framework and has no comments.

Intro:
Zend Framework is one of the php framework. If you are new to php frameworkss, you can think of framework as a pre defined arrangement of files and folders. It is like a ready made skeleton, to which you need to add body parts.

Where can you download it?
You can get the framework as zip or tar.gz file from http://framework.zend.com/download/latest. When I am writing this, I downloaded and tried 1.11.4 version. That was the latest one put up there.

There are two types of packages. Full and Minimal. I will go with the Full package. Before downloading, check out the requirements on the zend site.

How to install it?
There are few ways you can install it. Actually there is no .exe which will install and copy the files to program files directory or any where. You just need to extract and copy only some files.

When you extract the zip file, you need to bother about two directories mainly.

1. library folder, which contains only one subfolder - Zend. Within that, there are many subfolders. Those are actually classes that we will use in our projects. Some are standalone, some are inter dependant.
2. bin folder, which contains important batch scripts, that you will use to create base files that form the application framework.

Configuring Zend library folder
Many of the classes in library folder will be used frequently, so php should be able to lookup into this folder, without specifying include() so many times. One way is to add the library path (for eg: /home/ks/PHPFrameworks/ZendFramework/library, NOT till the Zend subfolder) to the include_path variable in php.ini. Remember to restart the webserver.

Shared web hosts, like Godaddy.com, dont have zend framework installed. There is a trick you can play with. I will tell you shortly, when I create the first application with zf command, the fantastic command line tool.

Configuring zf.sh on linux (ubuntu)
On ubuntu, the zf.sh can be configured with alias command. alias zf=/home/ks/PHPFrameworks/ZendFramework/bin/zf.sh
So next time you can just type zf to access this fantastic command line tool.

Configuring zf.bat on windows
Add the bin path to the PATH environment variable. Right click on My computer > Properties > Advanced > Environment variables > PATH. Add the bin path to the end.

Check zf
From the command line just type zf or zf.bat. You should see a big list of multiple commands and their syntax.

Note:
On Ubuntu, apt-get installs Zend Server community server in /usr/local/zend,
zf.sh is present in /usr/local/zend/share/ZendFramework/bin.
apache htdocs folder is present in /var/www/

Pages: [1]

•   Databases (6)
•   Diary (117)
•   HTML and Javascript (2)
•   For Your Information (18)
•   iSpecial Jokes (13)
•   Java related (1)
•   Javascript (1)
•   Jokes (41)
•   Mind Views (22)
•   My Observations (8)
•   Open Source Tech. (7)
•   PHP (6)
•   My Poetry (10)
•   Reviews (2)
•   Sing Along (21)
•   Sankaracharya Stotras (3)
•   My Talent (1)
•   Uncategorized (27)
•   Webservers (3)
•   Websites to see (28)
•   WML and WAP (3)
•   Zend Framework (10)

•   It is my personal site. Content included here is according to my wish. If you don't like my site, close it and go away.
•   'Mind Views' and 'My Observations' are purely opinions of mine and not others. If something in this site hurts you, control
     your mind and send me a mail. We will discuss. If you go to court of law, it's your wish. Every citizen has a right to express, so do I.
•   Some content in this site might not be appropriate for some audience, whose opinions differ with me. So don't try to argue with me.
•   Important: Don't try to judge me just by glancing this website. I am different outside.
•   This is a time-pass site. Reading it in a hurry will be useless.