Archive

Author Archive

symfony restful interface setup

March 5th, 2010 1 comment

How to make a restful interface easily with symfony:

# “project_root”/app/”app_name”/config/routing.yml

# default rules
homepage:
  url:   /
  param: { module: test, action: index }

test_get:
  url: /v2/tester
  class: sfRequestRoute
  param: { module: test, action: get }
  requirements:
    sf_method: [get]

test_put:
  url: /v2/tester
  class: sfRequestRoute
  param: { module: test, action: put }
  requirements:
    sf_method: [put]

test_post:
  url: /v2/tester
  class: sfRequestRoute
  param: { module: test, action: post }
  requirements:
    sf_method: [post]

test_head:
  url: /v2/tester
  class: sfRequestRoute
  param: { module: test, action: head }
  requirements:
    sf_method: [head]

# generic rules
# please, remove them by adding more specific rules
default_index:
  url:   /:module
  param: { action: index }

default:
  url:   /:module/:action/*

Remember, order DOES matter… if you have the default one at the top, the more specific matches to be overlooked. So move more specific items to the top, and your routing will be fine.

Summary:
post to http://localhost/sf_project/frontend_dev.php/test, will call frontend->test->post

delete to http://localhost/sf_project/frontend_dev.php/test, will call frontend->test->delete

put to http://localhost/sf_project/frontend_dev.php/test, will call frontend->test->put

head to http://localhost/sf_project/frontend_dev.php/test, will call frontend->test->head

you get the point…

Clean example of using APC caching

December 3rd, 2009 No comments

A nice clean example of using APC.

class bar{
    private $ttl = 300;  // number of seconds time to live.

    public function foo($val) {

        $key = sprintf('%s-%s-%s', __CLASS__, __FUNCTION__, $val);
        $result = apc_fetch($key);

        if (!$result) {
            // do logic here;
            $result = 'something';

            apc_store($result, $key, $this->ttl);
        }

        return $result;
    }
}

The inclusion of the class, and function name, is to help organize your cache. Thus giving you the ability to clear only part of your cache.

Hope this helps…

Categories: PHP Tags:

Find SVN directories and remove them

October 20th, 2009 No comments

http://www.lloydleung.com/2009/06/17/find-svn-directories-and-remove-them/

Useful linux command to remove svn data, when you need to clean it up for some reason.

Note: Use sparingly.

Categories: Uncategorized Tags:

phpdocs sniplet

September 3rd, 2009 No comments

Quick way of using phpDocs to generate php programmer manuals

$> phpdoc -f file1,file2,…,fileX -t manual -s -pp

or

$> phpdoc -d dir1,dir2,…,dirX -t manual -s -pp

$> phpdoc -h
#always works too…

Categories: PHP Tags:

shared dev environments… don’t DOs.

April 6th, 2009 No comments

SV…. thanks…

If you’re about to do the following, notify others.  Or people start yelling at you.

  1. Drop tables/columns/data
  2. Restart the database / HTTPD / other services

Or… alternatively, make your own sandbox, and you can play all you want…

Categories: Uncategorized Tags:

PHP5 simple type hinting

February 16th, 2009 No comments

The symptom error messages may include:

must be an instance of string, string given
must be an instance of integer, integer given

The cause:

“Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn’t supported.” — http://ca.php.net/oop5.typehinting

Summary:

You can’t type hint STRING or INT, at least as of PHP 5.2.8, the current release at the time of this post.

Categories: PHP Tags:

Override php.ini to turn on error reporting, and display errors

February 13th, 2009 1 comment

If you’re server doesn’t allow you to show error in php try the following lines at the near the top of your script.

Try this:

<?php
// Report all PHP errors
error_reporting(E_ALL);
ini_set("display_errors", 1);

If it still doesn’t show up, the web server daemon may not have write permissions to the default logging directory.

Categories: PHP Tags: , ,

Excellent post on programmer mistakes

February 8th, 2009 No comments

Great posting of stupid things programmers do… The * stupidest things I’ve done in my programming job

In all honesty, I think most programmers should make these kinds of mistakes on their own, and then learn from them.

One that does not understand the wrong, cannot become better.

To extend the post, specifically for PHP:

1. ORM

One should use Doctrine, or propel.  I personally favour Doctrine.

2. EAV

Doesn’t need to be PHP specific.

3. Database Access

PDO with Doctrine, can’t really go wrong…

4. IDE

Use an PHP IDE, there are lots out there now.

Eclipse PDT, NetBeans PHP, PHPEditor, Aptana… etc…

I currently favour NetBeans.  I have tried Eclipse PDT, Aptana, NetBeans… and I’m currently using NetBeans, and loving it.

5. Transactions

Just use them.  This reminds me to clean up, the data injector to use  transactions.  I know I can… I don’t know why I didn’t.

6. Prepared Statements

Prepared statements in PDO, or subsequently Doctrine, is trivial.

PDO example:

<?php
// the following statements has question marks inside the query...
// meaning, that those will be replaced with values later.  Think of the command as a combination of sprintf, and mysql_real_escape_string, together... but smarter.

$query_string = 'select * from tableA where id = ? and column_a = ?';

$this->prepared_statement = $this->DBH->prepare($query_string);

$this->prepared_statement->execute(array(123, "some random value, but will auto quote everythign for me even if it has \" and ' inside the string"));
$results  = $this->prepared_statement->fetch();  // or
$results2 = $this->prepared_statement->fetchAll();

Doctrine is an ORM, so if you’re using Doctrine, basically everything is prepared for you.

7. Business Logic

LOL, sorry… this one makes me laugh, as code at work has this type of “design” (or lack of) in legacy code.  This legacy code is less than a year old.  Thank you Botched Code!

Categories: General Programming Tags:

Wrapping CDATA

December 26th, 2008 No comments

function clean_up_xml($xml_string='', $array_of_tags)
{
    if (count($array_of_tags) == 0)
    {
        throw new Exception('Second parameter, must be an array of strings');
    }
    foreach ($array_of_tags as $tag)
    {
        // Clean up tags, and trim whitespaces around the tag.
        $needle[]  = "@[\n\r\t ]*(\<[/]?{$tag}\>)[\n\r\t ]*@";
        $replace[]  = '$1';
        // find tags that don't have CDATA, and wrap them with CDATA
        $needle[]  = "@\<{$tag}\>(?!\<\!\[CDATA\[)(.*?)\</{$tag}\>@ms";
        $replace[]  = "<{$tag}><![CDATA[$1]]></{$tag}>";

        $xml_string = preg_replace($needle, $replace, $xml_string);
    }
    return $xml_string;
}

Explanation:

  1. Remove all spaces, tabs, \r, and \n characters around the desired tag, replace with just the tag itself
  2. Find all occurances of $tag, but not followed by <![CDATA[, and wrap contents in <![CDATA[ ... ]]>.
Categories: PHP Tags:

found myself doing the start of a botched code…

December 3rd, 2008 No comments

“XSLT is a pain in the ass”. That was coming out of my mouth… until, you start understanding it. Best tool for the job, XSLT. It’s designed to style XML…

The solution to my problem was easy once you see samples. My problem was XSLT scoping was never discussed in much detail, in any example I saw.

I may eventually write a little code bit, for future reference.

Categories: Uncategorized Tags: