debuggable

 
Contact Us
 
52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60

Dessert #13 - A simple Config Model

Posted on 20/9/06 by Felix Geisendörfer

A lot of you probably like the idea of storing some of your applications configuration in a database since this way you can easily tweak things at run time versus having to edit configuration files on the server.

For me I found it pretty useful to just have a simple table called configs which contains two fields: "name" and "value". "name" is the primary key and it's type is varchar(255). The "value" fields type is text. After setting this up, I created a simple Model called Config and added two convenient little function named get & store to it which I can call from my controllers.

The code for the Config Model is this:

class Config extends AppModel
{
    var $name = 'Config';    
    var $primaryKey = 'name';
    var $validate = array('name' => VALID_NOT_EMPTY);
   
    function get($name, $default = null)
    {
        $config = $this->findByName($name);
        if (isset($config['Config']['value']))
            return $config['Config']['value'];
        else
            return $default;
    }
   
    function store($name, $value)
    {
        return $this->save(array('name' => $name, 'value' => $value));
    }
}

And usage is as simple as this:

class AppController extends Controller
{    
    var $uses = array('Config');
   
    function beforeFilter()
    {
        $this->theme = $this->Config->get('theme', 'summer');
    }
}

So in the example above the beforeFilter tries to get the value of 'theme' out of the configs table and in case that fails (theme isn't set), it will return 'summer' as default value.

--Felix Geisendörfer aka the_undefined

 

Dessert #12 - Debugging requestAction

Posted on 19/9/06 by Felix Geisendörfer

You can regard this as a general php debugging tip, but I found it pretty useful when either working with Object::requestAction() (you probably mostly use it from within the controller which extends object), or when having problems to understand a function call sequence in general.

The basic problem is that sometimes you want to find out which function called the function you are in right now, or even see the entire sequence of function calls that led to your current function being executed. In that case php's debug_backtrace() comes really handy. However the array it returns is a little too long and complex for what you usally need. In my case that is a list of Class::function()'s that were called. So what I did was to write a little snippet that I can insert by using a shortcut of my IDE. The code itself looks like this:

$backtrace = array_reverse(debug_backtrace());
foreach ($backtrace as $trace)
{
    debug(@$trace['class'].'::'.@$trace['function'].'()');
}

Usally when I paste it, it's 1 line long, but I split it up so you can read it a little better on here. For those of you who use some advanced debugging extensions or additional classes, of course, this will seem like a joke, but I believe in light weight debugging, because as soon as you need huge debugging frameworks you are in huge trouble ; ). So feel free to post your approaches to debugging function sequences, but keep in mind that this is only ment to be a quick & dirty snippet I trigger by pressing a hotkey, but which has served me well so far ; ).

--Felix Geisendörfer aka the_undefined

 

The 48 Minutes challenge / A rush of productivity

Posted on 18/9/06 by Felix Geisendörfer

For those of you who have followed this site in the past days you might have noticed that I was writing a lot of posts on here recently. This of course has something to do with my 10 Days of Free Dessert! series, but on saturday and sunday both, I wrote 3 desserts even so the bet just says I got to write 1 / day.

In the past 3-4 weeks, I've started to work hard on myself. I've been trying to get up at 5am every day and even so I still oversleep at least once a week and feel a little sleepy when getting up, it has given me a serious increase in productivity. Together with that I've pretty much given up drinking coffee especially in the morning. I still drink a cup in the afternoon from time to time, but gone are the days of waking up by the brute force of my Geeky 16 ounce coffee cup.

Even so all of this has really been benifital it wasn't until last saturday that I discovered a real killer strategy for increasing my productivity. It was a post on lifehacker.com, and it suggested to Work in 48 minute increments. So you would always work for 48 minutes on a certain task while making it your main priority to fight distraction. After that you take a 12 minutes break before you start to work again. This entire pattern has really pushed my productivity way up. On the past weekend when I started to apply it, I wrote 6 good sized posts on here, refactored (almost rewritten) the complete application I work on right now to increase code quality, did all my homework (I never do that ... ^^), met with friends and also completed a ton of minor tasks. Especially the first two items are pretty big and normally would have taken me 4-6 days instead of two. Now a lot of people who commented it negativley on it, but for me this just seems to work like a charm, so in case you are interested in trying it out as well, here are some advices I would like to give you:

  1. Use a coundown timer to keep track of the 48 minutes, otherwise you'll get of path easily. I personally love using my watch for it, but maybe you want to get yourself a big countdown timer instead.
  2. In the 48 minutes of work, *really* blend out all distractions. No email checking, no news reading, no instant messaging, no banging your head against the monitor. Force yourself to stay on task - you know after 48 minutes you'll definitly get a break.
  3. If you feel the urge to go to the toilet please go, I don't want to be responsible for any accidents ; ).
  4. If you get distracted by the outside world like a phone call or things like this, stop the timer. If you feel like you'll have some control over how long the distraction will be, remember where you stopped the clock and set it to the new value you try to stay with. After you are done, get right back on task.
  5. You'll maybe notice that this technic can lead to serious pressure of trying to finish the task in the given time, maybe even setting the timer back to get more time. Don't do that, if the task can't be done in 48 minutes take a break and start in 12 minutes again. This will make sure you don't exhaust yourself too badly for the hours to come.
  6. If you have to eat something or such, take a bigger break. Maybe 30, maybe 40 minutes. However, set the timer for this time as well to make sure you're not using it as an excuse for goofing of after a while.

Ok, if you wonder *why* 48 minutes: I don't know. It probably works just as well with any other combination. I'll probably experiment with that a little bit in the future.

PS: Here is a similar post, but this strategy has a good list of people who will especially benifit from this technic (like me).

--Felix Geisendörfer aka the_undefined

 

Dessert #11 - Welcome back, Friendly URL's

Posted on 18/9/06 by Felix Geisendörfer

Not too long ago you've all heard me saying Bye, bye Friendly URL’s. Now, after a lot of feedback and some more thoughts on it I came to what I find a nice compromise between REST'ness, SEO'ness, and FRIENDLY'ness.

For those of you who didn't read the old post, the basic problem was that it's very nice and convenient to have full control over your url's. So instead of having /posts/view/1 you might prefer /a-cool-thing or /post/a-cool-thing or something else, more friendly to both human's and search engines. However, the problem you get, is that you easily break bookmarks & links to your sites when you change the title of the post. And yet another issue is that this method requires all title fields to be unique. Now you can work around all of those, but what I decided in the Bye, bye Friendly URL’s-article was that it is too much of a hassle and you loose several advantages like the ease of building web api's.

So for my current project I started to rethink the issue again and remembered the idea brought up by tomo (and Peter Goodman). Instead of using the post title to identify a post in the url, just use it as 'decoration' together with the real id. So for example you could use urls like this in your application: /post/1/my-cool-post. Your action will only check the first parameter (1) in order to identify the post, so if the title ever changes, the link will still work, or to say it differently /post/1* will all lead to the same page.

What I didn't like about this structure was that for both, search engines, and people it could look like "my-cool-post" is a sub-item of /post/1/ which isn't the case. So a couple days ago I came up with a syntax I really like a lot:

/post/1:my-post-title

Maybe it's just personal preference. But to me it has all the good stuff:

  • When the title changes, all other urls (/post/1:*) will remain usable
  • The hierarchy is correct: "my-post-title" is the "1"st "post"
  • It's REST'ful: /post/1 instead of /posts/view/1 (which is RPC)
  • ... and, I think it just looks very pretty ; )

So however you may want to structure your url's, here is how to accomblish this stuff in CakePHP. The first thing is a route that will redirect your /post/* url's to PostsController::show();. My example is from CakePHP 1.2's Router, however it should work just the same with the old one:

Router::connect('/post/*', array('controller' => 'posts', 'action' => 'show'));

In your PostsController::show() action you'll just need a tiny bit of logic to split the id and post title (which I call url_suffix since I have an additional field for it in my project to gain more control over it):

function show($id)
{
    @list($id, $url_suffix) = preg_split('/[:]/', $id, 2);        
    $post = $this->Post->findById($id);
   
    $this->set('post', $post);
}

Ok and for those of you who want to get even fancier here is an improved version:

function show($id)
{
    @list($id, $url_suffix) = preg_split('/[:]/', $id, 2);        
    $post = $this->Post->findById($id);
   
    if ($url_suffix!==$post['Post']['url_suffix'])
    {
        $this->redirect('/post/'.$post['Post']['id'].':'.$post['Post']['url_suffix'], 301);
        exit;
    }    
   
    $this->set('post', $post);
}

The difference between this function and the first one is that the function checks if the $url_prefix that was in the user request is the same one as currently assigned to our post. In case it has changed (and our user is using an old bookmark for example), we redirect him to a page with the proper url_suffix attached to the url. This is not all that important for normal users, but it's very efficent for telling search engines that your page has moved.

Oh and I'll post a function that'll convert "This is my post" type of titles into url ones like "this-is-my-post". It's not all that difficult, but I want to make sure it takes care of a lot of eventualities (Umlaute/Utf8 chars, double spaces, ...).

--Felix Geisendörfer aka the_undefined

 

A horrible suspicion

Posted on 17/9/06 by Felix Geisendörfer

I think I just made pretty bad discovery a minute ago. For some reason I've always been pretty bad with names of Locations, People, etc., but for some reason I almost always remember the first letter correctly. So if I met a girl named "Sandra" and see hear again I would be like "Hey S ... S ... S ... S-Sorry, what was your name again?". Ok, no, I actually figured out that pretending to remember the name works a lot better then that, but anyway, the problem still exists.

So as I said a minute ago a horrible suspicion came to my mind. What is if all of this had to do with my programming, or to be more exact my with IDE. For those of you who don't know, I come from a BASIC background and started to use Visual Basic (*sigh*) fairly early. One of the nice aspects of it was that they always had a really good IDE with auto completion, just like my current IDE. So my suspicion is, that the auto complete feature is what causes me to forgot names. If I have a class called Pictures, I just type P press Ctrl+Space and hit enter -> done. I do this approximitly 100-300 times a day, so I think this could seriously have something to do with my inability to remember names. Whenever I use an IDE without auto completion, I feel really uncomfortable and my productivity goes way down which seems to support this theory as well.

What do you guys think, anybody having similar experiences? Any ideas how to fix it? I should probably start using Notepad++ for my coding at least once a week : ).

--Felix Geisendörfer aka the_undefined

 
52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60