Posts Tagged programming

Accessing Your Local MAMP Dev Environment From VirtualBox

Want to know how to access your MAMP environment from within a virtual instance of Windows or Linux (like VirtualBox or VMware fusion or Parallels)? Read on!

You already have a local development environment set up using MAMP (MAMP uses port 8888 so your localhost is accessed at http://localhost:8888 and custom sites, assuming you have set any up, are available at http://whatever:8888).

Sadly you need to test your sites in Internet Explorer (or more interestingly you want to run in Linux and use cool tools like cachegrind) so you have set up a virtual environment to do this. I am using Sun’s Oracle’s fantastic, open source application VirtualBox. The question is, how do you access your local sites from within VirtualBox? Launching IE or your Linux browser and going to http://localhost:8888 does not work… hmmm.

Turns out, the address you need to point at is http://10.0.0.2:8888. That will get you to MAMP’s htdocs directory, i.e. your http://localhost:8888!

Now that is useful but it doesn’t get give you access to your custom sites. Here is how you access those!

  1. Open your hosts file. In Windows it is found here: C:\windows\system32\drivers\etc\hosts and in Linux it is found here: /etc/hosts
  2. Under the bit that says: 127.0.0.1 localhost add the following (using whatever your actual project is called): 10.0.2.2 whatever

That’s it! Now you can visit http://whatever:8888 using IE or any other browser from within your virtual Windows and/or Linux environments.

Example hosts file (all I added was the third line):

127.0.0.1 localhost
127.0.0.1 ubuntu-vm
10.0.2.2 mynewsapp

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
etc, etc...

And now in my virtual environment’s browser I can access:

http://mynewsapp:8888/

(which is hosted in my Mac’s MAMP instance! Sweet!)

     

, ,

8 Comments

Extending Zend_View to Implement a Concrete Function…

…without using a view helper*.

In a nutshell:

  1. Extend Zend_View
  2. Put your method in this extended class
  3. Instantiate the class (in your bootstrap for instance)
  4. Assign it to the ViewRenderer
  5. Pass that viewrenderer to Zend_Controller_Action_HelperBroker’s addHelper method
  6. Use it in your view

The details:

In /library/My/Zend/ create View.php:

class My_Zend_View extends Zend_View
{
public function _($string)
{
return Zend_Registry::get('translate')->_($string);
}
}

In your bootstrap:

require_once APPLICATION_PATH.'../library/My/Zend/View.php';

$view = new My_Zend_View();

$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

In your view:

echo $this->_("Hello");

Some extras. I have APPLICATION_PATH defined in index.php as:

define('APPLICATION_PATH', realpath(dirname(__FILE__)).'/application/');

and I have Zend_Translate instantiated in bootstrap.php like:

$translate = new Zend_Translate('csv', APPLICATION_PATH.'../library/Mine/source-en.csv', 'en');

and lastly, don’t forget to register $translate with the registry or that echo in the view file won’t work:

$registry->translate = $translate;

*Why not use a view helper? Well, you should, actually. The only reason for this solution is that the original need was for something that would work with the underscore notation used by Poedit. Apparently implementing a method named “_” doesn’t work in a helper so this solution extends Zend_View… adding an underscore method.

Sooo… if you should use a view helper what does that look like?
In /library/My/Helper/Translate.php

class My_Helper_Translate extends Zend_View_Helper_Abstract
{
public function translate($string)
{
return Zend_Registry::get('translate')->_($string);
}
}

In your bootstrap:

$view->addHelperPath("../library/My/Helper/", "My_Helper");

In the view:

echo $this->translate("Hello");
     

, ,

No Comments