desirepath41 / visualCaptcha-PHP

visualCaptcha for PHP
MIT License
101 stars 29 forks source link

Use with Laravel #16

Closed Metrakit closed 10 years ago

Metrakit commented 10 years ago

Im trying to use your library with my Laravel apps. I have tried to adapt this exemple with my code. But I dont know how to adapt this following callback to my controller :

$app->get( '/audio(/:type)', function( $type = 'mp3' ) use( $app ) {
    $captcha = new \visualCaptcha\Captcha( $app->session );

    if ( ! $captcha->streamAudio( $app->response, $type ) ) {
        $app->pass();
    }
} );

Here is my controller :

class CaptchaController extends BaseController {

    /**
     * Start Captcha
     */
    public function start($params)
    {
        $session = new SessionCaptcha();
        $captcha = new Captcha($session);
        return $captcha->generate();
    }

    public function audio()
    {
        $session = new SessionCaptcha();
        $captcha = new Captcha($session);

        //$response = Response::make();
        $response->header('Access-Control-Allow-Origin', '*');

        //return var_dump($response);
        return $captcha->streamAudio($response, 'mp3');
    }

}

The start function works but not the audio function... I dont know how to add the first parameter to "$captcha->streamAudio($firstParam, $extension)".

BrunoBernardino commented 10 years ago

Hi, @Metrakit.

Can you tell me a bit more about which Laravel version are you using?

Do you have a proper route set for the audio that accepts something like /audio/mp3 and /audio/ogg ? I see you're forcing mp3 on your code, but you really shouldn't as that will make visualCaptcha's accessibility not work in some browsers.

A route as something like:

Route::get( 'audio/{type?}', array('uses' => 'CaptchaController@audio') );

and in your audio method, change it to:

public function audio( $type = 'mp3' ) {
// ...
return $captcha->streamAudio($response, $type);

If that still doesn't work, please share a bit more of your code so we can properly test it and play around with it.

Metrakit commented 10 years ago

Hi,

Thanks for your help.

The version of Laravel than I use it's the 4.2

I have declared my route like that:

Route::group(array('prefix' => 'captcha'), function()
{
    Route::get('/start/{params}', array('as' => 'captcha/start', 'uses'=>'CaptchaController@start'));
    Route::get('/audio', array('as' => 'captcha/audio', 'uses'=>'CaptchaController@audio'));
});     

I should precise the params in my route ? But the problem is not here, it's about the "$response". How i can declare it ?

I got this error before: "The Response content must be a string or object implementing __toString()"

BrunoBernardino commented 10 years ago

The route for audio needs the /{type?} part because I don't think it's called at the moment if the URL is /audio/mp3, and your route only allows /audio.

The first parameter is an array that will get the headers set. If there's nothing like that for Laravel (would have to catch up on documentation, haven't used it in a while), try sending an empty array, but that might have mimetype issues.

Metrakit commented 10 years ago

Ok thanks, I will try it

BrunoBernardino commented 10 years ago

@Metrakit any update?

Metrakit commented 10 years ago

Hi, Yeah I have finally successfully integrated VisualCaptcha with my Laravel app. It was only a stupid error by my fault about the Response class in Laravel. I shouldnt return directly that in a controller : "return $captcha->generate();". So now It work. But I have added some modifications to your library for I can use with Laravel. So I will can share with everyone the specific library than I have adapted for Laravel with a fork of your repository. For Laravel we have to use some basic functions of this framework to make it compliant and for have the best practices.

Thanks for this amazing library ! I'm bored of the Google's captcha and I wouldnt have to create by myself my own captcha's script.

Metrakit commented 10 years ago

Hi, Here is a Laravel version of your library : https://github.com/Metrakit/VisualCaptcha-Laravel

Cordially,

Metrakit.

BrunoBernardino commented 10 years ago

Thanks, I added it to https://github.com/emotionLoop/visualCaptcha