开发者

Calling symfony helper from a php script needed for Ajax?

N00b question:

I'm using Symfony with Doctrine. I have a form where I've added a little Jquery & Ajax check of whether a username that has been inputted into the form already exists. The jQuery calls a short PHP script on my web root that does the MySQL database check and returns true/false, which is then used to determine what error message to show.

However, if possible, I'd like to move the database call to a symfony helper function that then 开发者_StackOverflowreturns the true/false, rather having all the connection info etc on my web root. But I can't seem to be able to call the helper function correctly - all other components of the functionality work fine.

This is what I've got in my little php script:

$availability = GenericHelper::checkUsername();  // a public static function
echo $availability; // returns true/false to Jquery

Anyone? Thanks.


Did you correctly load the helper?

sfContext::getInstance()->getConfiguration()->loadHelpers('Generic');

Then you should be able to call checkUsername() directly.

Or you can try to put GenericHelper.class.php to the lib/ directory, this way it will be auto-loaded.


Helpers are not meant for that sort of job (they are only meant for "view" related work, that is, generate html, etc).

Instead your should create an action (either in the same controller as your form or in a dedicated ajax controller, I personaly find the first option more easily maintenable) that will do the username check using a method of the model. See http://gist.github.com/286797 for a code example on how to do that (please note this code has not been tested and therefore could not work ootb).

You would then direct your Ajax query to that action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜