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
).
Have a nice day…
9 comments so far
Leave a reply
your solution is far from safe, as you don’t escape input from the client. if I’d now call http://domain/project-dir/action/phpinfo/ I’d be able to see your full server configuration. Hell, in this way I’d be able to call stuff like eval() and other highly insecure functions.
The idea is nice but caution is required.
I have done this in a similar way but using classes and it’s methods as actions.
I like the ruby language syntax and ruby on rails is nice and I like that kind of coding style so I try to adopt some of it to PHP when I have to work in that language.
Why not just use one of the billion MVC frameworks that exists out there? Zend Framework, Symphony, Cake PHP etc.
@Stefan
I create and use action map to filter out user’s actions. Yes, caution is really required. Thank for your decent comment…
@Patrik
Yes, OOP is more suitable for this kind of task. I hate OOP but I already have a plan to improve to OOP. Thank you…
@Alex
I just love RoR and I just try to copy it. I mean I try to make my own RoR clone… Thank you too…
If you’re trying to make a RoR clone, have a look at both CakePHP (nearly full clone) or symfony (less of a RoR clone, but better set up and using PHP5 already).
CakePHP is like Ruby On Rails for PHP. Nate Abele said this himself on a conference recently.
How exactly do you plan to improve OOP? It’s fairly well-established
What is wrong with OOP? and you said you love RoR which is built in Ruby which is extremely object oriented.
I agree that reinventing the wheel is good for learning but to use something in production CakePHP, Symfony or CodeIgniter are good well tested frameworks ( I only have experience in CodeIgniter thought )
Good Site brother