Archive for the 'development' Category

Creating Action Controller in PHP

We could handle actions in PHP like this…

<?php

$actions = $_GET['action'];

switch( $actions ) {

case ‘hello’:

say_hello();

break;

case ‘bonjour’:

say_bonjour();

break;

default:

echo ‘Unknown action ‘.$actions;

}

?>

But this way is hard to extend and hard to maintain. How about adding say_konichiwa() action? We need to add a function and need to add a case in switch statement. For database management system, we need at least 6 actions. Show List View, Add New, Show, Edit, Update and Delete. If we put all those actions to one page like about example, it will be ugly and hard to read. And, we definitely will need to add another action.

We could make Ruby on Rails style Action Controller in easy way. First we need to create .htaccess for RESTful url.

.htaccess

RewriteEngine on
RewriteRule ^action/([a-z0-9\-\_]*)$ index.php?do=$1 [NC]
RewriteRule ^action/([a-z0-9\-\_]*)/$ index.php?do=$1 [NC]

Then, we need to create three scripts. index.php, actions.php and action_controller.php.

index.php

<?php include(‘actions.php’); ?>
<html>

<head><title>Index</title></head>

<body><?php actions(); ?></body>

</html>

actions.php

<?php

include(‘action_controller.php’);

function say_hello(){

echo ‘Hello World!’;

}

function say_bonjour(){

echo ‘BONJOUR !!!’;

}

?>

action_controller.php (the trick)

<?php

function actions(){

$action = $_GET['do'];

if($action == ){

if(function_exists(‘index’)){

index();

} else {

echo ‘There is no index action !’;

}

} else {

if(function_exists($action)){

//calling function same name with $action

$action();

} else {

echo ‘Unknown Action ‘.$action;

}

}

}

?>

That all…

Now, we can access our say_hello actions with…

http://domain/project-dir/action/say_hello/

If we want to add say_konichiwa action, we can simply put say_konichiwa function to actions.php and we can call our action from browser with ( http://domain/project-dir/action/say_konichiwa ). That is easy to maintain, easy to extend and nice (i think :D).

Have a nice day…

The Problem with GET Requests

Many web developer using a link to trigger actions (like this <a href=”delete.php?id=123″>). I already knew that kind of GET request link is dangerous. But I didn’t clearly understand the usage of POST and GET. Yesterday, while I continue my reading in “Agile Development with Rails”, I found out an interesting chapter “The Problem with GET Requests”.

Almost since HTTP was invented, it was recognized that there is a fundamental difference between HTTP GET and HTTP POST requests. Tim Berners-Lee wrote about it back in 1996. Use GET requests to retrieve information from the server, and use POST requests to request a change of state on the server. The problem is that this rule has been widely ignored by web developers.

See, we shouldn’t use GET request to changing a state on server. How many time do we breaking that rules? We should only use GET request to retrieving data from server. We already using GET request for many occasions in changing request such as deleting, adding and updating database or creating files on server. According to the about rule, we shouldn’t use GET for those kind of requests. A GET request link can cause easily user error. We should used form and button rather than link. Could we possible to use form for every state changing request? I don’t think so. But, if we want to do a dangerous action like “Firing Missile”, we really shouldn’t use the GET request link. We could use confirmation page with form. We will have advantages by using form. When user refresh the browser, the browser will warn that there was a POST request. When we can’t use a form, should create a GET request link with effective confirmation and we should certain that the reference page can’t refresh by user.

Top tips every programmer should follow

10 Programming Languages You Should Learn Right Now

  1. PHP
  2. C#
  3. Ajax
  4. Javascript
  5. Pearl
  6. C
  7. Ruby and Ruby on Rails
  8. Java
  9. Python
  10. VB.NET

Top Ten of Programming Advice NOT to follow

  1. Use error codes instead of exception
  2. Use unsigned integers for values that can only be positive
  3. Design classes parallel to their physical counterparts
  4. Make sure your team shares a common coding standard
  5. Write lots of comments
  6. Use accessors or properties rather than public fields
  7. Use the singleton pattern for variables that you KNOW you should have only one instance of
  8. Be tolerant with input and strict with output
  9. Code all the corner cases immediately, cause otherwise you’ll never go back and fix things
  10. Design first, then code

If you are a project manager or team leader, you should answer those question.

The Joel Test: 12 Steps to Better Code

  1. Do you use source control?
  2. Can you make a build in one step?
  3. Do you make daily builds?
  4. Do you have a bug database?
  5. Do you fix bugs before writing new code?
  6. Do you have an up-to-date schedule?
  7. Do you have a spec?
  8. Do programmers have quiet working conditions?
  9. Do you use the best tools money can buy?
  10. Do you have testers?
  11. Do new candidates write code during their interview?
  12. Do you do hallway usability testing?

Six ways to write more comprehensible code

  1. Comment like a smart person
  2. Use #define a lot. No, a LOT
  3. Don’t use variable names that will mock you
  4. Do error checking. You make errors. Yes, you
  5. “Premature optimization is the root of all evil.” - Donald Knuth
  6. Don’t be too clever by half