GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Friday, April 15, 2011

Zend - Example For Using Lucene

<?php

require_once "Zend/Controller/Action.php";
require_once "Zend/Search/Lucene.php";


class IndexController extends Zend_Controller_Action{

    public function indexAction(){
        $this->_redirect('index/front');
    }


    public function frontAction(){
        $index = new Zend_Search_Lucene('lola', true);
       
        $doc = new Zend_Search_Lucene_Document();
       
        $contents = array(
            'Google is greate one for our company. so when we are ready to make it, please make some time to be user for you',
            'Apple is another sompany for using with some simple of the example for this one.',
            'Microsoft is another one for the coming of the yeras for this one. It has windows',
        );
        $titles = array(
            'Google is technology company mobile',
            'Apple is mobile leader company',
            'Microsoft is king for desktop',
        );
        $links = array(
            'http://google.com',
            'http://apple.com',
            'http://microsoft.com',
        );
       
        for($i=0; $i<count($links); $i++){
            $doc->addField(Zend_Search_Lucene_Field::Keyword('link', $links[$i]));
            $doc->addField(Zend_Search_Lucene_Field::Text('title', $titles[$i]));
            $doc->addField(Zend_Search_Lucene_Field::Unstored('contents', $contents[$i]));
            $index->addDocument($doc);
        }
       
       
        $index->commit();
        echo $index->count()." Documents indexed";

    }
   
   
    public function renderAction(){
   
        $request = $this->getRequest();
        $query = $request->getParam('query');
       
        $index = new Zend_Search_Lucene('lola');
        $hits = $index->find($query);
       
        $output = '<div id="search">';
       
        $output .= '<p>Total Index: '.$index->count().' documents</p>';
        $output .= '<p>Search For: '.$query.' has resulted on '.count($hits).'</p>';
        foreach($hits as $hit){
            $output .= '<div id="item">';
            $output .= '<h2><a href="'.$hit->link.'">'.$hit->title.'</a> '.$hit->score.'</h2>';
            $output .= '</div>';
        }
        $output .= '</div>';
       
        echo $output;
    }   

}

// see more: http://devzone.zend.com/article/91

Share/Bookmark

Zend - Lucene Search

To create a search application using Zend, you use lucene class:

<?php
require_once "Zend/Search/Lucene.php";

$index = new Zend_Search_Lucene('lola', true);
The above code will create a directory folder for search index. There are five files for it:
1. read.lock.file
2. write.lock.file
3. segments_1
4. segments_2
5. segments_gen

$doc->addField(Zend_Search_Lucene_Field::Keyword('link', '<a href="#">Boo</a>'));
$doc->addField(Zend_Search_Lucene_Field::Text('title', 'Hello World'));
$doc->addField(Zend_Search_Lucene_Field::Unstored('contents', 'This is description'));
$index->addDocument($doc);
$index->commit();
echo $index->count()." Documents indexed";

For displaying the result:

$index = new Zend_Search_Lucene('lola');
$query = 'World';
$hits = $index->find($query);
echo "Index contains: ".$index->count()." documents.<br/>";
        echo "Search for: \"".$query."\" returned \"".count($hits)."\" hits";
foreach($hits as $hit){
     echo "<ul>";
     echo "<li>".$hit->link."</li>";
     echo "<li>".$hit->title."</li>";
     echo "<li>".$hit->score."</li>";
      echo "</ul>";
}
Share/Bookmark

Sunday, April 10, 2011

Zend - Linking CSS File

You can use either one of this below in your view file:
<?php
    $this->headLink()->appendStylesheet($this->baseUrl().'/css/style.css');
    echo $this->headLink();
?>

Or just use:
<link rel="stylesheet" href="<?php echo $this->baseUrl();?>/css/style.css"/>
Share/Bookmark

Zend - .htaccess

RewriteEngine On
RewriteRule .* index.php

Instead of using the above .htaccess file, use this below one:
RewriteEngine On
RewriteCond $1 !^(index\.php|img/*|js/*|css/*)
RewriteRule ^(.*)$ index.php/$1 [L]
Share/Bookmark

Zend - Built In Helpers

Helper is a function that you call from views.

1. Button Helpers
<?php
    $attr = array(
        'style' => 'background: yellow; border: 1px solid #ccc;',
    );
    echo $this->formButton('init', 'Init', $attr);
?>
Share/Bookmark

Zend - Zend Helper

File: application/views/helpers/HelloHelper.php

<?php

require_once 'Zend/View/Helper/Abstract.php';

class Zend_View_Helper_HelloHelper extends Zend_View_Helper_Abstract{
    function helloHelper(){
        return 'Hello Comes From Helper';
    }
}

?>

Then in the views file, call it using:
<?php echo $this->helloHelper();?>
Share/Bookmark

Zend - Authentication

    public function authAction(){
        $request = $this->getRequest();
        $registry = Zend_Registry::getInstance();
        $auth = Zend_Auth::getInstance();
       
        $db = $registry['db'];
       
        $authAdapter = new Zend_Auth_Adapter_DbTable($db);
        $authAdapter->setTableName('users')
                    ->setIdentityColumn('username')
                    ->setCredentialColumn('password');
                   
        // set the input credential values
        $uname = $request->getParam('username');
        $passwd = $request->getParam('password');
        $authAdapter->setIdentity($uname);
        $authAdapter->setCredential(md5($passwd));
       
        // perform the auth query, saving the result
        $result = $auth->authenticate($authAdapter);
       
        if($result->isValid()){
            $data = $authAdapter->getResultRowObject(null, 'passwd');
            $auth->getStorage()->write($data);
            $this->_redirect('/user/userpage');
        }else{
            $this->_redirect('/user/loginform');
        }
    }
   
    public function userpageAction(){
        $auth = Zend_Auth::getInstance();
       
        if(!$auth->hasIdentity()){
            $this->_redirect('/user/loginform');
        }
       
        $request = $this->getRequest();
        $user = $auth->getIdentity();
        $real_name = $user->real_name;
        $username = $user->username;
        $url = $request->getBaseURL().'/user/logout';
       
        $this->view->assign('username', $real_name);
        $this->view->assign('url', $url);
       
    }
   
    public function logoutAction(){
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $this->_redirect('/user');
    }

Share/Bookmark