Saturday, February 26, 2011

Zend Framework's Zend_Application and Bootstrapping

I am trying to make sense of all these bootstrapping that happens in the Zend Framework so I finally had a chance to dig in to the source code and this is what I've learned:

Zend_Application is the big moma of the bootstrapping process. If you want to change you config file to an XML from the default INI you need to do it in Zend_Application. As an example, edit the Zend_Application instantiation in public/index.php to make as an XML.

public/index.php
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.XML'
);

Within __construct initialization of Zend_Application, Zend_Config class is called. You need to take a look at /Zend/Application.php->_loadConfig() to understand and from within the class depending upon the extension of your config file (for us its .XML) it will load Zend_Config_Xml or Zend_Config_Ini as default.

Back to the bootstrap process. We go back to Zend_Application as the resources from Zend_Config are initialized.

If you are pretty new to Zend Framework and looked at the default .ini file you might get overwhelmed on where in the world they refer all these resources and extra configurations. I was not able to find it quickly in the official ZF doc but here is the link http://framework.zend.com/manual/1.11/en/zend.application.core-functionality.html

In any case, I will mention the default options here. You can see all these in the source code of  Zend_Application->setOptions(). Again i am talking about the application.ini file that is set through the bootstrap process.

//application.ini
phpsettings
includepaths
autoloadernamespaces
bootstrap

There is also one configuration that you may wonder where it came from:
appnamespace = "Application"

The appnamespace is initialized when the Zend_Application_Bootstrap_BootstrapAbstract->_construct is run. it will proxy to setOptions() method and this will look for any setters within the Bootstrap class namespace and Zend_Aplication_Bootstrap_Bootstrap->setAppNamespace() will be initialized automatically.

You can also set your own options. Mine has the Smarty parameters:


smarty.layout_dir = APPLICATION_PATH "/layouts/scripts"
smarty.views_dir = APPLICATION_PATH "/modules/front/views/scripts"
smarty.dir = APPLICATION_PATH "/../library/My/Smarty"
smarty.template_dir = APPLICATION_PATH "/../tpl/templates"
smarty.compile_dir = APPLICATION_PATH "/../tpl/templates_c"
smarty.config_dir = APPLICATION_PATH "/../tpl/configs"
smarty.cache_dir = APPLICATION_PATH "/../tpl/cache"
smarty.caching = 0
smarty.compile_check = true

Resources:
You can get the available resources at http://framework.zend.com/manual/1.11/en/zend.application.available-resources.html
I wont list the resources because theres a bunch of them. If your unfamiliar with the syntax in config its something like:

resources.db.adapter = PDO_MYSQL
resources.db.params.host = 'localhost'

These resources are just proxying to Zend_Aplication_Resource_Db (notice the _Db at the end of the class) which then ports the Zend_Db::factory().
Heres the adapter reference: http://framework.zend.com/manual/1.11/en/zend.db.adapter.html

You can access all this config options in your bootstrap file via
// Bootstrap.php
$this->getOptions();

You can also access your options and resources in the controllers:


 // inside your controller
$bootstrapClass = $this->getInvokeArg('bootstrap');
$smartyConfig = $bootstrapClass->getOption('smarty');
// Database resource
$dbResource = $bootstrapClass->getResource('db');

Thats about it for now

Friday, February 25, 2011

Tip: Strip characters at the end of the string in PHP

rtrim('http://yahoo.com./', '/'), ltrim('/jab/', '/')

Thursday, February 24, 2011

NOTES: Photoshop Canvas Size

This might be a newbie tip and maybe just a reminder for me in the future.
If you want to edit your container's size in photoshop without affecting or distorting the layers use
CANVASS SIZE instead of image size

Wednesday, February 16, 2011

Tip: Linux find remove logs

remove them logs:
find . -path "*.log.*.gz" -exec rm '{}' \;

Apache rewriteRules act differently on different hosts/shared hosts. A Zend Framework installation

I was testing the Zend Framework on a shared host and it happens that they do not allow modication of your Apache virtual host Document Root. Meaning if you are using a Zend Framework or some other CMS that requires you to point to a directory within your document root say a sub directory named /Public you may have some problems.

To solve this problem this .htaccess worked on my shared host:
credits too: http://www.alberton.info/zend_framework_mod_rewrite_shared_hosting.html

RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""

RewriteRule ^.*$ public/index.php [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ public/$1 [NC,L]
RewriteRule ^public/.*$ public/index.php [NC,L]

The above .htaccess worked fine in my Shared host but DO NOT work in my own dev box running Ubuntu with PHP as an Apache module. I keep getting "Request exceeded the limit of 10 internal redirects due to probable configuration error." problems in Apache.

To rectify this i have to edit the .htaccess again to:

RewriteEngine On
RewriteRule ^\.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ public/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ public/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^public/.*$ public/index.php [NC,L]
I also  have to "retain" the original .htaccess that comes from ZF in the Public directory where as in my shared host i have to REMOVE it or I will keep getting Error 500.

To conclude, I have no idea why different rewrites work from the other. I dont know if its Apache version, PHP version, compilation whatevert. I am pretty much clueless actually but try to mix match i guess if things doesnt make sense?

Sunday, February 13, 2011

Problem Saving Persistent Network Connections in Windows 7

If you try to map a network drive in Windows 7, you may have experience that it wont connect to it upon reboot of your system, even if you check "save credentials".

I've google the solution for this problem but you may dig a lot of web sites to find it so ill lay it out here for someone who may need it.

Go to:
Start -> Control Panel -> User Accounts & Family Settings -> Credential Manager

You may see under Windows Credential you current mapped account credentials.
Click "Add a Windows Credentials" and enter your network account AGAIN. This will override your current credentials. That should fix the problem and should connect after reboot now.

The problem is with the persistence of you connection. You may notice under you credential's persistence that it may be set to "per session". It has to be set to "enterprise" and the solution above should fix that!

Friday, February 11, 2011

XML Encoding Issues

Thought it might be worth to document my experience today since I tend to forget most of the solutions for my problems.

It is important to be aware of XML limitations specially when it relates to encoding. When using UTF-8 since its only an 8 bit character it will not convert a lot of characters specially the ones coming from ISO-8859-1 html enitities. You will keep getting errors in your XML!! One solution is to change the encoding of your XML to match your encodings and the other is to convert these characters to UNICODE. I chose the latter solution.

Here's a usefull script I plucked in from PHP.net that will convert html entities to UNICODE (credits to php.net):

    public static function htmlentitiesToUnicode($input)
    {
      $htmlEntities = array_values(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES));
      $entitiesDecoded = array_keys(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES));
     
      $num = count ($entitiesDecoded);
      for ($u = 0; $u < $num; $u++) {
        $utf8Entities[$u] = '&#'.ord($entitiesDecoded[$u]).';';
      }
     
      return str_replace ($htmlEntities, $utf8Entities, htmlentities($input, HTML_ENTITIES));
    }

Another problem I encountered is within the transmission and reading of the XML data. I used PHP's SimpleXML to convert my XML to objects. One problem SimpleXML is giving me is eventhough the html entities are converted to unicodes, it is giving out jibberish characters as output! 

Heres a sample of what i am talking about:
Original encoding: résumés
UNICODE encoding (XML): r&#233;sum&#233;s
Jibberish SimpleXML translation: résumés

fortunately there is a simple solution for this problem....iconv()
just use
$val = iconv('UTF-8', 'ISO-8859-1', $val);

and hopefully that should solve your encoding problems.

Monday, February 7, 2011

Tip: Enabling Apache Mod_Proxy

If you want to "mask" a URL say contents in http://myurl.com/index.php would pull the content of http://myurl2.com/index.php so when you view http://myurl.com/index.php you are actually viewing http://myurl2.com but URL stays in http://myurl.com. You can do this using Apache's Mod_Rewrite via Mod_Proxy.

You should also enable dependent modules like
proxy_connect.c
proxy_ftp.c
proxy_http.c


Then in your .htaccess in http://myurl.com/ you can do something like:
RewriteRule ^path/to/myurl/(.*)$ http://myurl2/$1 [P,L]

Remember the P flag which stands for proxy!!