CakePHP – rapid development in PHP

CakePHPLately I’m totally amazed by the beauty that is CakePHP. It’s one of those hyped »rapid webdevelopment frameworks« …and boy, it really enables you to work fast!
At least compared to what I was used to previously in my PHP development.

  • endless procedural code
  • crafting DB queries by hand
  • no MVC
  • no ORM
  • composing my own “frameworks” to encapsulate DB and HTML output
  • simple OO code with no real application structure

…well, you get the spin.

Basically, it meant ugly, unmaintainable code that needed hours of work before there was something to see for the client (or myself) not even talking about being finished.

During the last four days I got myself to sit down and dive into the CakePHP Manual and learn the framework. It was really enlightening and suddenly PHP coding is fun again :)
I was able to put together the whole structure of a new app, I’m working on, just by cleverly planning the DB layout and mapping this structure to the models. From there it was just a few simple commands on the shell to let Cake generate the controllers and views. After deleting the ones I didn’t need, I could immediately start tweaking the business logic and fine tune the views. It’s a really good feeling seeing how fast the development is progressing.

Yesterday I took a quick glimpse at Ruby On Rails, just to see where it all came from. The CakePHP developers claimed to have built their framework like RoR but, oh my god – it is just exactly like RoR. The concept of convention over configuration is perfectly migrated from Ruby to PHP.
I kinda like that, because it means if I know Cake I can quickly and very easily learn Ruby On Rails, just by learning a bit of different Ruby syntax, all the other concepts are already in my head :)

I’m excited to see where it goes from here, it really looks like my productive output could get a serious boost from now on!

LastVenue – toying around with RSS

Out of pure boredom and because I did enough work on my thesis today (…and because there wasn’t anything decent on TV), I decided to distract myself with a little coding. This is what came out:

LastVenue

Just download it, rename the filename ending to .php, configure it by pasting in the last.fm URL of your desired location and upload it to your webserver. You will also have to download SimplePie that takes care of the all the RSS stuff and place it in the same directory as the LastVenue script.
If you want to include it into a PHP script paste in the following:
include('lastVenue.php');

This script pulls all events for a specific venue from last.fm and lists them in a simple, CSS styleable unorderd list. The script is meant to be included into a webpage and thus features none of the other HTML markup to make it into a full document. It basically only echoes out a headline and a few list items.

Oh yea, of course the script is Microformats enabled ;)
Microformats enabled

Note: the AJAX loding functions got skipped, because I’m really tired right now. Maybe they’ll show up in version 2.0 …who knows?

Demo

include("/home/flo/www/blog/wp-content/uploads/lastVenue.php");
?>

Simple PHP RSS Script

In case anybody needs to quickly publish DB contents as RSS feed, you can now use my SRSSS – Simple RSS Script :)
Nothing sophisticated but should serve a valid RSS 2.0 feed. Just fill in the setup vars on the top, construct your SQL query and make sure you’ve got the right values in the while-loop.

Download here -> SRRS

Installing eAccelerator on Debian

This is a quick HowTo for installing the eAccelerator PHP cache/optimizer on a Debian system (should be applicable to other Linux distros as well).

  1. Download the latest release from SourceForge.net to your server and extract the sources.
  2. Change to the sources directory and type in the following.
    export PHP_PREFIX="/usr"
    $PHP_PREFIX/bin/phpize
    ./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config

    This should set up the environment and compile configuration correctly.
  3. Now compile with make and install with make install (as root) afterwards.
  4. To configure eAccelerator you have to edit your PHP config which resides in /etc/php4/apache2/php.ini on my system.
    Add the following lines at the end: extension="eaccelerator.so"
    eaccelerator.shm_size="16"
    eaccelerator.cache_dir="/tmp/eaccelerator"
    eaccelerator.enable="1"
    eaccelerator.optimizer="1"
    eaccelerator.check_mtime="1"
    eaccelerator.debug="0"
    eaccelerator.filter=""
    eaccelerator.shm_max="0"
    eaccelerator.shm_ttl="0"
    eaccelerator.shm_prune_period="0"
    eaccelerator.shm_only="0"
    eaccelerator.compress="1"
    eaccelerator.compress_level="9"

    The file README in the sources directory gives a pretty good explanation of all those parameters. The only one you should probably alter is the shm.size because 16MB of shared memory might be a little bit too conservative.
  5. Last step is to create the directory where eAccelerator stores the cached scripts which don’t fit into the shared memory. mkdir /tmp/eaccelerator
    chmod 0777 /tmp/eaccelerator

Now you’re done. Further tweaking can be accomplished by altering all the parameters in the php.ini file.

Custom Apache Errors

Ich habe mich endlich mal dran gemacht ein paar spassigere HTTP Error Messages für den Apache hier zu basteln. Die standardmässigen sind einfach zu langweilig. Hirschy war so nett mich mal mit ein paar netten Sprüchen zu versorgen.

Die Fehler werden alle von einem Script abgefangen, dem der Statuscode übergeben wird.
>>> Sourcecode

Im Apache muss nur noch in der httpd.conf für jede abgefangene Fehlermeldung ein Eintrag gemacht werden:

# Custom Response Messages
ErrorDocument 402 http://no-panic.at/error.php?e=402
ErrorDocument 403 http://no-panic.at/error.php?e=403
ErrorDocument 404 http://no-panic.at/error.php?e=404
ErrorDocument 405 http://no-panic.at/error.php?e=405
ErrorDocument 406 http://no-panic.at/error.php?e=406
ErrorDocument 407 http://no-panic.at/error.php?e=407
ErrorDocument 409 http://no-panic.at/error.php?e=409
ErrorDocument 410 http://no-panic.at/error.php?e=409
ErrorDocument 411 http://no-panic.at/error.php?e=411
ErrorDocument 414 http://no-panic.at/error.php?e=414
ErrorDocument 415 http://no-panic.at/error.php?e=415
ErrorDocument 417 http://no-panic.at/error.php?e=417

—————————————————–
Wichtig!
Die Direktive für den Fehler 401 MUSS ein lokaler Redirect sein, da sonst nicht der richtige Response an den Client geschickt wird und daher vermutlich keine Aufforderung zur Passwort eingabe erfolgt.
—————————————————–

Leider hab ich keine Möglichkeit gefunden, wie man das ganze bewerkstelligen kann ohne einen externen Redirect zu machen (mittels http://…) sodass es trotzdem für alle Domains auf diesem Server gilt. Wenn jemand Ideen hat, wäre ich sehr erfreut :) (evtl. mittels mod_rewrite ?).
Wenn man intern weiterleitet hat man nämlich den Vorteil, dass etliche Variablen vom Server gleich gesetzt werden, man müsste also nichtmal den Response Code per GET übergeben, sondern könnte ihn direkt in PHP per $_SERVER['REDIRECT_STATUS'] auslesen. Zusätzlich würden noch einige andere sehr interessante Variablen gesetzt werden, die man dann weiterverarbeiten kann.

AnschlieÃ?end noch ein paar Beispiele:

Vorschläge zu weiteren Error Messages sind bitte erwünscht!
Auch Ideen, welche Bilder man bei den jeweiligen Messages verwenden könnte.
Fragen, Wünsche und vor allem Ergänzungen/Anmerkungen zum PHP Code sind ausdrücklich erbeten ;)