Yii CAPTCHA url is broken

 ̄綄美尐妖づ 提交于 2019-12-01 22:03:43

问题


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

So, my model for user's registration is User. There is a validation rule:

array('code', 'captcha', 'allowEmpty'=>!Yii::app()->user->isGuest),

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

In my next code:

$this->widget('CCaptcha', array(
    'clickableImage'    => true,
    'showRefreshButton' => false,
))

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',

But after that captcha wants to load from an url

http://site.loc/user/captcha/v/4fe99aca1bbed

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

Is there an easy solution to solve this? Should I repair URL's or re-configure CAPTCHA?


回答1:


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

All controllers for common users are (of course) in Frontend section, including UserController and MainController.

Yes, I wrote some configs to hide /frontend/ in URL's like this:

'<c:\w+>/<a:\w+>' => 'frontend/<c>/<a>',

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',

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)

And I got an error:

CCaptchaValidator.action "captcha" is invalid. Unable to find such an action in the current controller. 

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)

So, now my AJAX Captcha validation works correctly where I want.

Good luck with Yii's default CAPTCHA, dear community!



来源:https://stackoverflow.com/questions/11206715/yii-captcha-url-is-broken

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!