Categories
PHP

Zend Framework : Session Handling, URL Routing, URL Helper and Redirecting

Some basics of Zend Framework, I am documenting in case I forget these in the future 😉

Session Handling

URL Routing
Add in Bootstrap:

URL Helper

URL Helper is available via the method – url() in a view object. The method takes an array of options as its first parameter. The second parameter is optional where you can pass a route object name (eg. ‘category’ from the above url routing example). This comes in handy when you want to construct routed URLs.

By default the helper takes the following options: “action”, “controller” and “module”. If anyone of them is not provided, default one is used. It can also take in GET params which will be appended to the URL in a SEO friendly manner. If you pass a route object, you can simply pass the dynamic parts, no need to pass the entire action, controller or anything.

Note that: In the second case, “id” itself is passed to the URL. But in the first case, the URL routing rule has been honored.

Inside a view script, you can directly use:

Since it is a member of the View object, you can also access it from an action like this:

Handling Redirection
Zend has a Redirector helper which provides advanced features when it comes to redirections. But for most cases, the built in _redirect() method of an action will do the job if used wisely with the URL helper.

That’s it! Zend Framework is verbose and usually has more than one way to achieve something. It’s up to you to decide what suits your needs!

Categories
Python

Running django on Site5

It took me an entire day, yes it did! I went here : http://wiki.site5.com/Python/Django/Django_installer. Browsed the forums. No help! Then I started on my own. I noticed that virtual python is loading the modules from the main python lib (/usr/lib/python2.4) instead of the local copy. I imported sys and read the sys.modules list. It pretty much made sense. If python doesn’t run locally (inside the virtual environment), it will try to install easy_install into the server wide path. You can’t achieve that unless you’re the root.

While looking for an alternative, I found virtualenv. It not only worked, it also shipped with a built in easy_install.

Later, when I set things up, I noticed that my app was breaking because Python wasn’t elegant enough to let use codes like this:

Huh! I had to browse the django svn and try all the versions before I got it working. It was a tiring process, but I learned a lot of things! Here’s a quick tutorial for fellow Site5 users:

1) Get Virtualenv from here: http://pypi.python.org/pypi/virtualenv

2) Run this command:

3) The above command will create a virtual python environment for you in “~/bin”. You can now run Python and easy_install by typing:

4) Now get MySQL bindings for Python and the flup package for the FCGI dark spells 😉

5) Now we are ready to rock and roll with Django! Remember, Site5 has Python 2.4. The latest builds of Django might break. Please use the one I used in the following script:

6) Create some Symlinks:

7) Create a dedicated directory for django projects:

8 ) Lets create a project and an app:

9) Create django.fcgi with the following content:

10) Lets finish with a .htaccess to finish it off, should we? 🙂