Yii CAPTCHA网址已损坏 [英] Yii CAPTCHA url is broken
问题描述
我想在我的Yii项目上创建一个AJAX注册表格。因此,在每个页面上我都有一个登录按钮,如果用户未被授权,则会显示一个弹出窗口。在该弹出窗口中,他可以看到带有电子邮件字段,密码字段和CAPTCHA(默认Yii验证码)的注册表格。
I want to create an AJAX-registration form on my Yii-project. So on every page I have a login-button, which shows a popup if the user isn't authorized. In that popup he can see registration form with email field, password field, and CAPTCHA (default Yii captcha).
因此,我用于用户注册的模型是User。有一个验证规则:
So, my model for user's registration is User. There is a validation rule:
array('code', 'captcha', 'allowEmpty'=>!Yii::app()->user->isGuest),
我的控制器,其中所有用户的动作是,是User(url:site.url / user /),我在其中添加了验证码操作:
My controller, where all user's actions are, is User (url: site.url/user/) and I added there captcha action:
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
'foreColor'=>0x000000,
),
在我的下一个代码中:
$this->widget('CCaptcha', array(
'clickableImage' => true,
'showRefreshButton' => false,
))
这是一个问题:(位于弹出窗口中,可以在每个页面(索引页面,例如,属于主控制器)验证码想要从该控制器(从/ main / captcha而不是/ user / captcha)加载图像。
And here is a problem:( Being inside a popup, which can be shown on every page (index page, for example, which belongs Main controller) captcha wants to load an image from that controller (from /main/captcha, not /user/captcha).
在每个控制器中添加验证码是个坏主意,所以我添加了
Adding captcha in every controller is bad idea, so I added
'captchaAction' => '/user/captcha',
但是之后,该验证码想从URL加载
But after that captcha wants to load from an url
http://site.loc/user/captcha/v/4fe99aca1bbed
用户友好的URL的验证码崩溃了,我无法避免(因为我认为,不是yii专家,这是由于常见的path-config)。
User-friendly url's crashed captcha and I can't avoid it (as I think, not being a yii-expert, it's because of common path-config).
有一个简单的解决方案可以解决此问题吗?我应该修复URL还是重新配置CAPTCHA?
Is there an easy solution to solve this? Should I repair URL's or re-configure CAPTCHA?
推荐答案
因此,在等待帮助的过程中,我自己解决了问题:)
在我的项目中,我将控制器分为下一个层次:
So, while I was waiting for help, I solved the problem by myself :) In my project i separated my controllers into the next hierarchy:
/frontend
/backend
(当然)所有用于普通用户的控制器都位于Frontend部分,包括UserController和MainController。
All controllers for common users are (of course) in Frontend section, including UserController and MainController.
是的,我编写了一些配置来隐藏/ frontend /在URL中,如下所示:
Yes, I wrote some configs to hide /frontend/ in URL's like this:
'<c:\w+>/<a:\w+>' => 'frontend/<c>/<a>',
我敢肯定,那个字符串在我的验证码配置中('captchaAction'=>'/ user / captcha')知道要去哪里(几乎是这样!),但是不!
And i was sure, that string in my CAPTCHA config ('captchaAction' => '/user/captcha') knows where to go (and it was almost so!), but NO!
我应该将完整路径写入我的控制器并执行以下操作:
I should write FULL PATH to my controller and action like below:
'captchaAction' => '/frontend/main/captcha',
正如我所说的,我将验证码操作放在UserController,但现在您可以看到它位于MainController中(我从UserController中删除了它)
As I said in question, I placed my CAPTCHA action in UserController, but now you can see it is located in MainController (i deleted it from UserController)
我收到一个错误:
CCaptchaValidator.action "captcha" is invalid. Unable to find such an action in the current controller.
因此,要解决此问题,我只需要使用属性模型中的CCaptchaValidator的/www.yiiframework.com/doc/api/1.1/CCaptchaValidator#captchaAction-detail rel = nofollow> captchaAction !代码:
So, to solve this, I just had to use a property captchaAction of CCaptchaValidator in my User MODEL! Code:
array('code', 'captcha', 'captchaAction'=>'/frontend/main/captcha', 'allowEmpty'=>!Yii::app()->user->isGuest)
所以,现在我的AJAX验证码验证可以在我想要的地方正常工作。
So, now my AJAX Captcha validation works correctly where I want.
使用Yii的默认验证码,亲爱的社区,祝你好运!
Good luck with Yii's default CAPTCHA, dear community!
这篇关于Yii CAPTCHA网址已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!