search
Homephp教程PHP开发Yii2.0 creates a custom component method

下面是Yii2.0如何创建一个自定义组件的实例

第一步:在common下创建components文件夹

第二步: 在新建的components文件夹中创建一个自定义组件 比如:ReadHttpHeader.php,代码如下

) as $ip) {
                    if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                        return $ip;
                    }
                }
            }
        }
    }
 
}

注意: 'common' 已经在 common/config/bootstrap.php定义好了别名,可以直接使用

第三步:打开common/config/main.php(main-local.php) 在配置文件中添加你的组件

<?php
 
return [
          &#39;components&#39; => [
                    &#39;ReadHttpHeader&#39; => [
                              &#39;class&#39; => &#39;commoncomponentsReadHttpHeader&#39;
                    ],
          ],
 ];

第四步:现在我们的组件方法可以被所有控制器调用,比如我们现在在我们的基础控制器(BaseController)中加载我们的组件ReadHttpHeader ,其他控制器都继承我们的基础控制器

<?php
 
namespace frontendcontrollers;
 
use Yii;
use yiiwebController; 
class BaseController extends Controller {
 
    protected $session = false;
 
    public function actions() {
        return [
                  &#39;error&#39; => [
                            &#39;class&#39; => &#39;yiiwebErrorAction&#39;,
                  ],
        ];
    }
 
    public function init() {
 
        parent::init();
 
        // IP essential for prelim DDoS check
        if (!$this->cgS(&#39;UC-SEC.1a&#39;)) {
            $ip = Yii::$app->ReadHttpHeader->RealIP();
            echo $ip;
        }
    }
 
}

以上就是Yii2.0 创建一个自定义组件方法的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor