Creating Action Controller in PHP
We could handle actions in PHP like this…
$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
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
<html>
<head><title>Index</title></head>
<body><?php actions(); ?></body>
</html>
actions.php
include(‘action_controller.php’);
function say_hello(){
echo ‘Hello World!’;
}
function say_bonjour(){
echo ‘BONJOUR !!!’;
}
?>
action_controller.php (the trick)
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…
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…
Compiled Languages vs. Scripting Languages
Compiled Languages
Computers do not actually understand the code we write. We need to translate our human-readable code to machine-readable code. Our code need to be translated into bits and bytes that can be executed by computers. This translation process is called compilation. Languages that require compilation are called compiled languages. C, C++ and Java are compiled languages.
Scripting Languages
Languages that not require compilation are called scripting languages. Perl, PHP, Python and Ruby are scripting languages. Those languages rely on our source-code all the time. Scripting languages didn’t have compiler or a compilation process. Those languages used interpreters to translate our source-code to machine executable code on the fly.
Compiled Languages vs. Scripting Languages
Mostly, we have been using compiled languages like C, C++, Java and Visual Basic. Java has an interpreter but it need to compile first. Earlier versions of Visual Basic were interpreted. Visual Basic 5 and later came with compiler. So Java and Visual Basic should be called as compiled languages.
Imagine; you wrote an application with Java. Then you compile your application with javac. After compiling completed, you run your application. When running your application, you notice a bug. To fix it, you have to stop your application, go back to source code, fix the bug, wait for the code to recompile, and restart your application to confirm that it is fixed. And if you find another bug, you’ll need to repeat that process again.
In a scripting language, you can fix the bug and just need to reload your application —no need to restart or recompile anymore. It’s as simple as that.
But, the scripting languages have a big performance problem. Translating on the fly can affect the application’s performance. Typically, the pre-compiled code is faster than on the fly translating. But, good news is, there are ways to speed up scripted languages, including techniques such as code caching and persistent interpreters.
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.
Why shouldn’t beginner learn VB first?
First of all, I need to say that I’m neither a guru nor expert. I have no idea to consider and compare about programming languages. But I think I can give some advice to beginner.
Yesterday, my friend asked me that he wanted to start learning programming. He wanted to know which language he should learn first. He heard about some visual languages. Someone told him, those visual languages are very easy to learn and will give him great productivity. “Is that true?” He asked. How should I say?
In my opinion, if the beginner learns Visual Basic first, it can misunderstand about the programming. I don’t mean that Visual Basic is a bad language. Visual Basic is the language of choice to develop desktop application for me.
Visual Basic is a useful language. But beginner shouldn’t learn that language first. It can only be wasting their time by trying to understand textbox instead of trying to understand string processing. They will make double click on a command button to write a command1_click() procedure but they can’t clear understand about object, method and event. They think Visual Basic itself a programming language and they also can’t clearly understand what programming language is and what IDE is. They will blame other languages for not having great drag and drop IDE. And then they won’t try to understand memory management, threading, garbage collection and other important programming feature. They will think those features are not necessary to understand.
After having some experiences in developing applications, they can develop some windows-base small applications. But they will face some difficulties in learning other languages like Java, PHP. Because they can’t develop without drag and drop IDE. And they will blame those languages again.
In the real programming world, it is impossible to emphasize on only one language. We must know various languages. If beginner learn VB and love it, they will find that difficult to learn other language. If s/he learns a language like C or Java first, studying other language will be easy-going.
Believe it or not, I have never heard about regular expression within 2 years experiences as a professional because I’m a VB developer.