mews 的 captcha 函数调用 与 路由访问 ?

<img class="thumbnail captcha" src="{{ captcha_src('flat') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="点击图片重新获取验证码">

captcha_src('flat') 函数源自 vendor/mews/captcha/src/helpers.php,它是如何将函数注册到全局 helpers 调用的?
另外,/captcha/flat 的路由是怎么访问的?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

这个就要从它的服务提供者看起,请查看 Mews\Captcha\CaptchaServiceProvider

 public function boot()
    {
        // Publish configuration files
        $this->publishes([
            __DIR__.'/../config/captcha.php' => config_path('captcha.php')
        ], 'config');

        // HTTP routing
        if (strpos($this->app->version(), 'Lumen') !== false) {
           $this->app->get('captcha[/{config}]', 'Mews\Captcha\LumenCaptchaController@getCaptcha');
        } else {
            if ((double) $this->app->version() >= 5.2) {
                $this->app['router']->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha')->middleware('web');
            } else {
                $this->app['router']->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha');
            }
        }

        // Validator extensions
        $this->app['validator']->extend('captcha', function($attribute, $value, $parameters)
        {
            return captcha_check($value);
        });
    }

路由就是在上面的方法中注册的。至于helpers 函数,那事 composer 的自动加载文件,查看这个库的composer.json

"autoload": {
        "psr-4": {
            "Mews\\Captcha\\": "src/"
        },
        "files": [
            "src/helpers.php"
        ]
    },

应该看明白了吧。

5年前 评论
讨论数量: 2

这个就要从它的服务提供者看起,请查看 Mews\Captcha\CaptchaServiceProvider

 public function boot()
    {
        // Publish configuration files
        $this->publishes([
            __DIR__.'/../config/captcha.php' => config_path('captcha.php')
        ], 'config');

        // HTTP routing
        if (strpos($this->app->version(), 'Lumen') !== false) {
           $this->app->get('captcha[/{config}]', 'Mews\Captcha\LumenCaptchaController@getCaptcha');
        } else {
            if ((double) $this->app->version() >= 5.2) {
                $this->app['router']->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha')->middleware('web');
            } else {
                $this->app['router']->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha');
            }
        }

        // Validator extensions
        $this->app['validator']->extend('captcha', function($attribute, $value, $parameters)
        {
            return captcha_check($value);
        });
    }

路由就是在上面的方法中注册的。至于helpers 函数,那事 composer 的自动加载文件,查看这个库的composer.json

"autoload": {
        "psr-4": {
            "Mews\\Captcha\\": "src/"
        },
        "files": [
            "src/helpers.php"
        ]
    },

应该看明白了吧。

5年前 评论

这个就要从它的服务提供者看起,请查看 Mews\Captcha\CaptchaServiceProvider

 public function boot()
    {
        // Publish configuration files
        $this->publishes([
            __DIR__.'/../config/captcha.php' => config_path('captcha.php')
        ], 'config');

        // HTTP routing
        if (strpos($this->app->version(), 'Lumen') !== false) {
           $this->app->get('captcha[/{config}]', 'Mews\Captcha\LumenCaptchaController@getCaptcha');
        } else {
            if ((double) $this->app->version() >= 5.2) {
                $this->app['router']->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha')->middleware('web');
            } else {
                $this->app['router']->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha');
            }
        }

        // Validator extensions
        $this->app['validator']->extend('captcha', function($attribute, $value, $parameters)
        {
            return captcha_check($value);
        });
    }

路由就是在上面的方法中注册的。至于helpers 函数,那事 composer 的自动加载文件,查看这个库的composer.json

"autoload": {
        "psr-4": {
            "Mews\\Captcha\\": "src/"
        },
        "files": [
            "src/helpers.php"
        ]
    },

应该看明白了吧。

5年前 评论

@Jeffrey 大神啊 做个记录

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!