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…

28 comments so far

  1. Stefan on

    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.

  2. Patrik on

    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.

  3. Alex on

    Why not just use one of the billion MVC frameworks that exists out there? Zend Framework, Symphony, Cake PHP etc.

  4. Ei Maung on

    @Stefan

    your solution is far from safe, as you don’t escape input from the client.

    I create and use action map to filter out user’s actions. Yes, caution is really required. Thank for your decent comment…

    @Patrik

    I have done this in a similar way but using classes and it’s methods as actions.

    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

    Why not just use one of the billion MVC frameworks that exists out there?

    I just love RoR and I just try to copy it. I mean I try to make my own RoR clone… Thank you too… 🙂

  5. Stefan on

    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).

  6. kvz on

    CakePHP is like Ruby On Rails for PHP. Nate Abele said this himself on a conference recently.

  7. Jasper on

    How exactly do you plan to improve OOP? It’s fairly well-established

  8. Patrik on

    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 )

  9. Dr.Ox on

    Good Site brother

  10. Konnie21 on

    Various fields of our life utilize lots of time, thence why should you expend valuable time for comparison contrast essay accomplishing? This is better to utilize some professional comparison essay service to purchase the analysis essay from, I think so.

  11. tooftadvodo on

    I’m a new member here and I thought it’s time to share with you guys something.

    I stumbled upon this great website which lets all of us about Reb Shlomo

    I know that Rabbi Shlomo Carlebach is in serious demand currently as a spot to stay at in the promised land therefore I just figured this surely would be able to be good share.

    Also if someone is wanting to get Reb shlomo carlebach Songs orstyle probably this is the coolest place to get it, surely you agree?.

    Hope that this valuable info helps people.

    have a good day
    James http://serve.mysmiley.net/party/party0014.gif

    http://rebshlomo.org/i//shlomoshoa300x175.jpg

  12. jones on
  13. geoge on
  14. Wzrwdqlh on

    I love the theatre tiny dirty teens jdeov

  15. Hblpwtxz on

    Can I call you back? teen porn excitement 9002

  16. Nqjqjesu on

    Could I make an appointment to see ? young nymphet
    lefoln

  17. Abixqhbm on

    Do you like it here? girl ning pthc
    swtle

  18. Hjdccywr on

    Directory enquiries nn pre teen
    qyg

  19. Slhtwios on

    What sort of music do you listen to? Naked Preteen Nymphets Galleries
    :-[[[

  20. Tqvdhelc on

    I’ve been made redundant Kds Preteen >:-PP

  21. Plnohndb on

    A staff restaurant http://kiqedeteno.de.tl young 18 models Yea right.. her mom took her vids so she could jerk off to them. I’d like to see her suck a fat cock. Giggity.

  22. Uotuszpp on

    I’d like to open a personal account http://mupicyekioc.de.tl picture bbs pantyhose i am going to fuck her ass and lick her cunt and drink her cum and lick her tits and finger her and ram my dick btween her tits and cum down her throat

  23. Qtlbbomp on

    Yes, I play the guitar http://ohyryafy.de.tl kiddie bikini model This pretty much ruined her for me. I used to like her. But multiple cocks is the biggest closet faggot fantasy. A straight man should want to see more cunt, not cock. Seriously? What the fuck? Why did I click, my boner is killed, and no jerking off. Fuck this.

  24. maplesyrupghost on

    @ the spammers.. just wow.

  25. metabolic cooking login on

    Good post. I learn something totally new and challenging on sites I stumbleupon on a daily
    basis. It’s always helpful to read articles from other authors and use a little something from other sites.

  26. cadomagenge on

    This is very useful information shared here. I am really thankful for this

  27. Robpob on

    Социальная сеть – найди своего сослуживца http://vayklass.com – Фильмы про Чечню

    http://vayklass.com – Чечня фото
    http://vayklass.com – Смотреть видео Чечня
    http://vayklass.com – Ищу друзей

  28. Amit Kumar on

    Your detail is really helpful for me to develop a website or php development.


Leave a comment