Zend Framework

Archive for March 19th, 2009

Ubuntu: Working with text editor (vim)

Posted by Faheem Abbas on March 19, 2009

If you are following my blog, you will find my last few articles are on Ubuntu.

Yeah, that’s where I am working now.

I’ve installed Ubuntu and working to have some knowledge, coz I’m going to develop my own extension for php in next few weeks.

Before going to get my hands dirty with extension development, I will need to get a bit used to the Ubuntu. I have already learnt some commands like how to install and update apache, php and other stuff. However until now I was unable to write something using any text editor. Thought I tried to use text editor by going to Application/Accessories/Text editor, however I was unable to create document other than my home directory. So I decided to use vim. I is never an easy job to work with vim if you are newbie.

Let’s tell you what I have learnt so for.

  1. To open a file you will need to write the following command in you shell terminal.

$ sudo vim /etc/resolv.conf

The above command will open if file already exist or will create new one.

  1. To make changes to the file you will need to press “Insert” key of your keyboard.
  2. You can use arrow keys to move left, right, up and down. You can use h, j, k, l for this purpose too.
  3. Once you write something in the file. Press “escape” key. This will take you to the command mode. Its necessary to go to the command mode for saving the document.
  4. After pressing the “escape” key, press colon(:). This will take you to the end of the file.
  5. write :wq for save and quite. And press enter at the end.
  6. you can you use i for going to “insert” mode and a for append mode.

That’s it for now. Hopefully I’ll learn more next time and will share with you too.

Posted in Ubuntu | 5 Comments »

Zend Framework: working with layout and views (two step view)

Posted by Faheem Abbas on March 19, 2009

In this article I’m going to discuss layout and views and will give a practical example.

You may have heard of two step view.

The term two step view is used when we use layout and view templates for making our page look and feel.

Mostly you will need header, menu and footer same for the entire web application, only contents in the center of the page changes. So if this is the case you can get benefits out of two step views.

Create a layout containing header, menus and footer etc and view will have the contents relevant that specific page.

To implement two step view  in Zend framework you will need to make some configuration in bootstrap file.

Add the following lines to your bootstrap file.

$options = array(

    'layout'     => 'layout_name',

    'layoutPath' => '/path/to/layouts',

    

);
 
$layout = Zend_Layout::startMvc($options);

Keep in mind two things.
1.    Specify correct layout_name.
2.    Path to the layout file must be correct.
I usually prefer to create my layout directory in scripts directory under application/views.
 
If you create layout.phtml in your layouts directory you will need to define it as follows.
 

<?php
        include "header.phtml";
        
?>
// view contents goes here.
<?php
        
        <?=$this->layout()->content?>
 
?>
// footer goes here.
<?php
        include "footer.phtml"
?>

        
Now whatever request (controller/action) you made to your page, header and footer will be included in the response.

There may be cases (in some of your actions) where you don’t want your header and footer be appeared in the look and feel.

So write in those actions, the following line of code

$this->_helper->layout->disableLayout();

After including this line in your action, only the view contents will appear. It mean the contents in the header and footer will not be served.

Some time you need to execute only action code and redirect your request to another action instead of showing anything.

Write the following code in this case

$this->_helper->layout->disableLayout();

$this->_helper->viewRenderer->setNoRender();

Another very important thing is when you call a specific action, Zend Framework render a template file with the name similar to the action name. for example if you request view action, Zend will look for view.phtml file in the specific template directory.

If you want to render other then the default template, view.phtml in the above example. Write the following in your action.

$this->render(‘thanks’);

Posted in Zend Framework | 5 Comments »