Zend framework tutorial, build your Blog

Create application/views/helpers/LoggedInUser.php

<?php
class Zend_View_Helper_LoggedInUser
{
    protected $_view;
    function setView($view)
    {
    $this->_view = $view;
    }
    function loggedInUser()
    {
        $auth = Zend_Auth::getInstance();
        if($auth->hasIdentity())
        {
            $logoutUrl = $this->_view->linkTo('auth/logout');
            $user = $auth->getIdentity();
            $username = $this->_view->escape(ucfirst($user->username));
            $string = 'Logged in as ' . $username . ' | <a href="' .
            $logoutUrl . '">Log out</a>';
        } else {
            $loginUrl = $this->_view->linkTo('auth/identify');
            $string = '<a href="'. $loginUrl . '">Log in</a>';
        }
        return $string;
    }
}
 

Change the contents of application/views/scripts/index/index.phtml with

<?php if (count($this->paginator)): ?>
<?php foreach ($this->paginator as $post ): ?>
<div class="post">
  <div class="title"><a href="<?php echo $this->baseUrl()."/posts/view/id/".$post['id']; ?>"><?php echo $this->escape($post['Title']); ?></a></div>
  <div class="description"><?php echo nl2br(substr( strip_tags( $post['Description']), 0 , 300)); ?></div>
  <div class="commentscount">Comments( count )</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
<div class="pagination"><?php echo $this->paginationControl($this->paginator,
                                    'Sliding',
                                    '/partials/my_pagination_control.phtml'); ?></div>

Change the contents of application/views/scripts/index/login.phtml with

<?php echo $this->errors; ?>
<?php echo $this->loginForm; ?>

Change the contents of application/views/scripts/index/logout.phtml with

<div>Successfully Logged out</div>

Create application/views/scripts/partials/my_pagination_control.phtml with (  You can place partials in a different folder too . I forgot to change that )

<?php if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
  <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
    &lt; Previous
  </a> |
<?php else: ?>
  <span class="disabled">&lt; Previous</span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
  <?php if ($page != $this->current): ?>
    <a href="<?php echo $this->url(array('page' => $page)); ?>">
        <?php echo $page; ?>
    </a> |
  <?php else: ?>
    <?php echo $page; ?> |
  <?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
  <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
    Next &gt;
  </a>
<?php else: ?>
  <span class="disabled">Next &gt;</span>
<?php endif; ?>
</div>
<?php endif; ?>
<?php
/*
 * Drop down paginaton example
 */
/*
?>
<?php if ($this->pageCount): ?>
<select id="paginationControl" size="1">
<?php foreach ($this->pagesInRange as $page): ?>
  <?php $selected = ($page == $this->current) ? ' selected="selected"' : ''; ?>
  <option value="<?php
        echo $this->url(array('page' => $page));?>"<?php echo $selected ?>>
    <?php echo $page; ?>
  </option>
<?php endforeach; ?>
</select>
<?php endif; ?>
<script type="text/javascript"
     src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js">
</script>
<script type="text/javascript">
$('paginationControl').observe('change', function() {
    window.location = this.options[this.selectedIndex].value;
})
</script>
*/?>

Change application/views/scripts/posts/add.phtml with

<div><?php echo $this->postForm; ?></div>

Change application/views/scripts/posts/edit.phtml with

<h2>Edit Post</h2>
<div><?php echo $this->postForm; ?></div>

Change application/views/scripts/posts/view.phtml with

<div class="title"><?php echo $this->post['Title']; ?></div>
<div class="description"><?php echo $this->post['Description'];  ?></div>
<?php
    $acl = new Model_Acl(); 
    $identity = Zend_Auth::getInstance()->getIdentity();
    if( Zend_Auth::getInstance()->hasIdentity() && $acl->isAllowed( $identity['role'] ,'posts','edit') ) : ?>
<div><a href="<?php echo $this->baseurl().$this->edit; ?>">Edit</a></div> 
<?php endif; ?> 
<div class="comments">
    <?php if( count($this->comments) ) : ?>
        <?php foreach( $this->comments as $comment ) : ?>
            <div class="postedby"><a href="<?php echo $comment['Webpage']; ?>" ><?php echo $this->escape( $comment['Name'] ); ?></a> on <span><?php echo $this->escape( date( 'd-m-Y', strtotime($comment['Postedon']) ) ); ?></span></div>
            <div class="comment"><?php echo $this->escape( $comment['Description'] ); ?></div>
        <?php endforeach; ?>
    <?php else : ?>
    <div>No comments</div>
    <?php endif; ?>
</div>
<div class="newcomments">
<?php if( Zend_Auth::getInstance()->hasIdentity() ) : 
        echo $this->commentsForm;
    else :
        ?>Login to Post comments<?php 
    endif;
?>
</div>

I hope I have not missed any when adding to the blog . If there is any erros  do let me know . You can grab the working copy from git , that will be good if there is some errors . I have got many helps from more than 50 blogs . I have made a custom search engine for the zend framework learning too . In the earlier post I have posted .

Thanks and I hope you will enjoy my small blog, and If there is any confusing part let me know it. You can post comments rather than directly mailing to me . You can grab the piece of code from http://github.com/harikt/zendblog and don't be shy as Jon Lembord of http://zendcasts.com says . . Thanks for the wonderful facility provided by github and git .

I have also posted a new post for those who are looking for a module and admin layout. Do check it and get the files from http://www.harikt.com/content/base-files-start-your-zend-framework-project . This may be useful for you to start your zend framework project .

Anonymous's picture

Thanks for the answer but nothing has changed...i point by browser to blog/pubic but i found only a blankpage...

hari's picture

I guess , something went wrong . May be you want to look whether all errors can be thrown from PHP . I mean initialise the display_errors and E_ALL .

Have you tried the Quick start? If you have tried that I hope that may have resolved most of the problems.

Thanks again.

Anonymous's picture

In the file: application/views/scripts/posts/view.phtml you have litered the code with open and close PHP tags.
Why do it that way?
In my opinion, the code be easier to read and cleaner if there was only one set of PHP tags. Just echo the HTML where required?

hari's picture

phtml file contains less php codes . Its the view, so the designers can quickly understand and change.
If you have once used smarty you would have understood. This is why we keep model in one place , controller in another and view in another . Else I would have places all in one :) . There are lots more benefits for MVC :). You may want to explore ;).

Anonymous's picture

This would get me excited if I'd seen some screenshots or heard some information about it, but at the moment, I really am not bothered about it at all.

Anonymous's picture

Hi,
The error message I get now is:

An error occurred
Application error

can you help me?
where could it possibly have been wrong?

Anonymous's picture

I have also tried it and it was working perfectly.
I have build a complete blog with the help of this tutorial.

Powered by Drupal, an open source content management system