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…
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
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.
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
nu9w8E http://djIjw3MnccVop6a5hFgql.com
kj3pYp http://fnYwlOpd2n9t4Vx6A3lbk.com
I love the theatre tiny dirty teens jdeov
Can I call you back? teen porn excitement 9002
Could I make an appointment to see ? young nymphet
lefoln
Do you like it here? girl ning pthc
swtle
Directory enquiries nn pre teen
qyg
What sort of music do you listen to? Naked Preteen Nymphets Galleries
:-[[[
I’ve been made redundant Kds Preteen >:-PP
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.
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
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.
@ the spammers.. just wow.
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.
This is very useful information shared here. I am really thankful for this
Социальная сеть – найди своего сослуживца http://vayklass.com – Фильмы про Чечню
http://vayklass.com – Чечня фото
http://vayklass.com – Смотреть видео Чечня
http://vayklass.com – Ищу друзей
Your detail is really helpful for me to develop a website or php development.