Re: Question on the best way to proceed and stay true to MVC/CakePHP philosophy
Set the name attribute into the CommandParser class (If your are using PHP 4 the name attribute is mandatory as it is in models and controllers). class CommandParserComponent extends Object { var $name = CommandParser; //the rest of the code. } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Question on the best way to proceed and stay true to MVC/CakePHP philosophy
if(function_exist( $this-{$command}()) ){ For testing the existence of cass methods, you should use method_exists instead. if (method_exists(get_class($this),$command) { //blah blah } With this fix i think that this idea would work too, --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Question on the best way to proceed and stay true to MVC/CakePHP philosophy
I am having some issues getting what you said to work. It must be a syntactical problem, I can't get something like CommandParser::processCommand() to return anything. Here is the code: /controllers/components/command_parser.php ? class CommandParserComponent extends Object { function processCommand($command) { return $command; //just to test if it's working } } ? /controllers/channels_controller.php var $components = array('RequestHandler','CommandParser'); ... function command() { $command = str_replace(array(\r, \n, \t, /), '', $this- data['Comment']['comment']); $result = CommandParser::processCommand($command); $this-set('command', $result); $this-render('view'); } This is just to test if it's working, and I can't get anything to print when command is called. I am almost positive I have followed what you suggested correctly, can't seem to figure this out. As always, any help is appreciated. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Question on the best way to proceed and stay true to MVC/CakePHP philosophy
To specify, here is the error: Fatal error: Class 'CommandParser' not found in /home/dave/www/ goontastic/app/controllers/channels_controller.php on line 61 So it's not seeing the 'class' but I am including it in the components array. This for 1.2 RC3. On Oct 15, 3:04 pm, Chez17 [EMAIL PROTECTED] wrote: I am having some issues getting what you said to work. It must be a syntactical problem, I can't get something like CommandParser::processCommand() to return anything. Here is the code: /controllers/components/command_parser.php ? class CommandParserComponent extends Object { function processCommand($command) { return $command; //just to test if it's working }} ? /controllers/channels_controller.php var $components = array('RequestHandler','CommandParser'); ... function command() { $command = str_replace(array(\r, \n, \t, /), '', $this-data['Comment']['comment']); $result = CommandParser::processCommand($command); $this-set('command', $result); $this-render('view'); } This is just to test if it's working, and I can't get anything to print when command is called. I am almost positive I have followed what you suggested correctly, can't seem to figure this out. As always, any help is appreciated. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Question on the best way to proceed and stay true to MVC/CakePHP philosophy
So I have an ajax chat application that I am working on. If a user types a comment the starts with '/' it sends the comment to the 'command' function in my controller (as opposed to a normal 'add' function for chats). For example if the user types /help it will list all the possible commands. Now the command function is going to have to parse the command and return the proper information. The way I did the first couple commands was just a switch statement. But I got to thinking if there was a better or more cake way to do it. Are there better ways then just a giant switch statement? Should I try to create my own component? What are your thoughts on this? Any help will be greatly appreciated. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Question on the best way to proceed and stay true to MVC/CakePHP philosophy
I think first that all you have to separate the parsing of the text and the execution of the command itself via components, namely: 1) Create a CommandParser component that has function to parse a command. 2) Create a CommandExecutive component that has a method for implement each of the commands. I suggest you to handle the user input in a unique action in the controlle. For example, create a send action where all the user text is sent: class ChatController extends AppController { var $components = Array('CommandParser','CommandExecutive'); /*handles all text send by user*/ function send($text) { /*analize wherter the input is a command or is plain text*/ if (CommandParser::isCommand($text)) { //is a command redirect for command execution //use the parser component to get the command and the parameter of the command $command = CommandParser::getCommand($text); $parameter = CommandParser::getParameter($te $this-redirect(/chat/command/$command/ $parameter); } else { //is normal text, show $this-redirect(/chat/add/$text) } } //here is the rest of actions function add ($text) { //perform logic of add text } function command ($command, $parameter) { //perform logic of command } } For executing the command you can avoid the big switching statement by using an array of callbacks indexed with the command names. All callbacks are methods defined in the ActionExecutive component itself. For example: //this function may be in the controller function proccessCommand($text,$parameter) { $command_list = Array ( 'help' = displayHelp, 'quit' = quitUser, /* ... */ ); /*verify if it is a valid command*/ if (empty($command_list[$text])) { return INVALID_COMMAND; /*this maybe a constant*/ } $callback = $command_list[$text]; /*execute the command that is a method in a command executive component*/ $result = CommandExecutive::$callback($parameter); return $result; } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Question on the best way to proceed and stay true to MVC/CakePHP philosophy
you can do a command controller, but anyway you should write the list of commands, there is a quick example (please correct it is just a guide) class commandcontroller extends appcontroller{ function index($command = 'error'){ if(function_exist( $this-{$command}()) ){ $this-{$command}() }else{ $this-error(); } } function commandOne(){ } function commandTwo(){} function error(){ echo the command doesnt exist; } } On Tue, Oct 14, 2008 at 6:50 PM, Chez17 [EMAIL PROTECTED] wrote: So I have an ajax chat application that I am working on. If a user types a comment the starts with '/' it sends the comment to the 'command' function in my controller (as opposed to a normal 'add' function for chats). For example if the user types /help it will list all the possible commands. Now the command function is going to have to parse the command and return the proper information. The way I did the first couple commands was just a switch statement. But I got to thinking if there was a better or more cake way to do it. Are there better ways then just a giant switch statement? Should I try to create my own component? What are your thoughts on this? Any help will be greatly appreciated. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Question on the best way to proceed and stay true to MVC/CakePHP philosophy
Thanks! This is amazing advice. I am such a beginner and this was fantastic. The array idea is so simple and brilliant, way easier. I will try to look into doing what you suggest. Thanks again. On Oct 14, 9:01 pm, ORCC [EMAIL PROTECTED] wrote: I think first that all you have to separate the parsing of the text and the execution of the command itself via components, namely: 1) Create a CommandParser component that has function to parse a command. 2) Create a CommandExecutive component that has a method for implement each of the commands. I suggest you to handle the user input in a unique action in the controlle. For example, create a send action where all the user text is sent: class ChatController extends AppController { var $components = Array('CommandParser','CommandExecutive'); /*handles all text send by user*/ function send($text) { /*analize wherter the input is a command or is plain text*/ if (CommandParser::isCommand($text)) { //is a command redirect for command execution //use the parser component to get the command and the parameter of the command $command = CommandParser::getCommand($text); $parameter = CommandParser::getParameter($te $this-redirect(/chat/command/$command/ $parameter); } else { //is normal text, show $this-redirect(/chat/add/$text) } } //here is the rest of actions function add ($text) { //perform logic of add text } function command ($command, $parameter) { //perform logic of command } } For executing the command you can avoid the big switching statement by using an array of callbacks indexed with the command names. All callbacks are methods defined in the ActionExecutive component itself. For example: //this function may be in the controller function proccessCommand($text,$parameter) { $command_list = Array ( 'help' = displayHelp, 'quit' = quitUser, /* ... */ ); /*verify if it is a valid command*/ if (empty($command_list[$text])) { return INVALID_COMMAND; /*this maybe a constant*/ } $callback = $command_list[$text]; /*execute the command that is a method in a command executive component*/ $result = CommandExecutive::$callback($parameter); return $result; } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---