How to get controller name or action in view or layout

To get the controller name in view or layout use the below function

 $controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName()

To get action

 $action =  $controller = Zend_Controller_Front::getInstance()->getRequest()->getActionName()


Zend Framework - Get the last url parameter

 In the controller,

   $requestUri = $this->getRequest()->getRequestUri();
   $lastParameter = substr($requestUri, strrpos($requestUri,'/')+1);

Using same view for different modules in zend framework

In the Controller Action we can specify the view path then use helper to load view in the specified folder

class IndexController extends Zend_Controller_Action{

 public function indexAction()
    {
        $this->view->setScriptPath(APPLICATION_PATH.'/modules/modulename/views/scripts/controllername');
           $this->_helper->viewRenderer->setNeverController(true)->setRender('index');
     }
}

I hope there is no default method like $this->_forward('module', 'controller', 'action')

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');