Using Zend Auth Login without using the Zend MVC

We can use the Zend Login for our existing website without using whole Zend Library. Zend Auth module provides the more secure login.

First we need to download the following folders and files from Zend Library
 1. Zend/Db/
 2. Zend/Auth/
 3. Zend/Session/
 4. Zend/Db.php
 5. Zend/Auth.php
 6. Zend/Session.php
 7. Zend/Exception.php
 Then you can use the below code in your login.
require_once('Zend/Db/Table/Abstract.php');
require_once('Zend/Auth/Adapter/DbTable.php');
require_once('Zend/Auth.php');
require_once('Zend/Db/Expr.php');





$params = array(
    'host'     => 'localhost',
    'username' => 'support',
    'password' => 'vanilla',
    'dbname'   => 'Training',
);

try {
    $db = Zend_Db::factory('Pdo_Mysql', $params);
    $db->getConnection();
} catch(Zend_Db_Adapter_Exception $e) {
    die("
There was an error connecting to the server
"); //Probably bad login or mysql daemon is not running.
} catch(Zend_Exception $e) {
    die("
Something failed to load
"); //factory() probably failed to load the adapter class
}
$uname = 'admin';
$paswd = md5($uname);
 $auth       = Zend_Auth::getInstance();
 $authAdapter = new Zend_Auth_Adapter_DbTable($db);
 $authAdapter->setTableName('users')
                   ->setIdentityColumn('username')
                   ->setCredentialColumn('pwd');  
       
       // Set the input credential values
       $authAdapter->setIdentity($uname);
       $authAdapter->setCredential(md5($uname));
   
       // Perform the authentication query, saving the result
       $result = $auth->authenticate($authAdapter);
   
       if($result->isValid()){
        print_r($result);
        print_r($_SESSION);
         echo "Success";
       }else{
         echo "Failure";
       }

How to use MySQL 'RAND()' with limit in with Zend Select Query

  We need to use new Zend_Db_Expr('RAND()') with select query
  Ex: $selectQry    =    $this->util->db->select()->from(array('P'=>$this->_name),array('package_id','name','day_duration','night_duration','price','discount_price'))
                                                    ->order(new Zend_Db_Expr('RAND()'))
                                                    ->limit(2);

Html email using Zend Framework

Create a template in the view /views/scripts/emails/package.phtml
html
body
 echo $this->packageType;
echo $this->details; 
/html

Then in the controller, from which you need to send this use the below code

// create zend view object
$html = new Zend_View();
$html->setScriptPath(APPLICATION_PATH . '/views/scripts/emails/');

// assign the view variables here
$html->assign('packageType', 'Luxure');
$html->assign('details', 'Package inclusions');

// create mail object with utf-8 encoding
$mail = new Zend_Mail('utf-8');

// render view
$bodyText = $html->render('package.phtml');

//Set emaill
$mail->addTo('shaijudavis@gmail.com');
$mail->setSubject('Welcome to Limespace.de');
$mail->setFrom('info@elegantkerala.com','Elegant Kerala');
$mail->setBodyHtml($bodyText);
$mail->send();

How to use MySQL MATCH AGAIST in Zend Framework Query

   We can use MATCH AGAINST with zend select where clause.
   Ex:
  $selectQry    =    $this->db->select()->from(array('D'=>$this->_name))
                                                     ->joinLeft(array('C'=>TBL_CATEGORIES),
                                                         'C.category_id = D.category_id',array('category_id'))
                                                    ->where('D.is_deleted = ?', 0)
                                                    ->where('MATCH(C.name) AGAINST (?)', $txt)
                                                    ->orWhere('MATCH(D.name,D.summary,D.description) AGAINST (?)', $txt)
                                                    ->group('D.destination_id');

How to get request path info in the controller?

To get the request pathinfo use the function $this->_request->getPathInfo() in the controller

How to disable the In Array validator in Zend Form?

When we use multiple select or checkbox option, there is default validator so that the value should not be empty. We can bypass it using the function setRegisterInArrayValidator
 
   Ex: $selCategories = new Zend_Form_Element_MultiCheckbox('categories', array(
                        'multiOptions' => $categories));
         $selCategories->setRegisterInArrayValidator(false);