@shakedko
IF AN EXPERT SAYS IT CAN'T BE DONE GET ANOTHER EXPERT.
- DAVID BEN-GURION

Zend Framework - How to redirect/get application base URL

What is exactly base URL ? 

Base URL is sub.domain.end, for example: 

If your full URI is http://www.yoursite.com/some/params/id/1, so base URL will be www.yoursite.com

Redirect to application base URL

Sometimes you would want to be able to redirect from one page to your main homepage, for example:

  1. User A enter Site B.com login page
  2. User B enter username and password and he had been successfully logged in
  3. User Bis being redirect to Site B.com

As I like to say, if you are reading this post, you would probably need some help about its subject. There are two ways:

<?php
public function loginAction(){
.
.
if (user is logged in successfully) {
    $this->_helper->redirector->gotoUrl();    //Option 1
    // OR setGotoUrl() uses gotoUrl() when no param is being passed
    $this->_helper->redirector->setGotoUrl(); //Option 2
}
.
.
}

 How do I get base URL string?  

<?php
public function loginAction(){
 $baseURL = $this->getRequest()->getHttpHost();
 /*
 When using redirect helper and full URL
 you will have to attach http(s)://
 which means you will have to use isSSL() method
 */
}
Note:

My suggestion is to create  BaseControoler that will handle your current HTTP request & base URL:

<?php
class BaseController extends Zend_Controller_Action{
  /**
   *  @var Zend_Controller_Request_Http $_request
       */
  protected $_request;
  protected function init(){
      //Save request
      $this->_request = $this->getRequest();
          //Save httpHost (base URL)
      $this->_httpHost = $this->_request->getHttpHost();
      $this->siteInit();
  }
  protected siteInit(){
      /*
          You don't really need it
          I just prefer to know that if someone creates
          another controller he won't have to call parent::init()
      */
  }
}

After creating BaseController you have to setup your own controller(s): 

<?php
//Extend BaseController
class IndexController extends BaseController {
public function indexAction(){
      var_dump($this->_httpHost,$this->_request->getParams());
  }
}

That's it for today, 

Shak

Work In Progress 🚧
Discipline