[moka同学笔记]Yii2 自定义class、自定义全局函数(摘录)

1、在app\components下新建MyComponent.PHP

    namespace app\components;  

    use Yii;
    use yii\base\Component;
    use yii\base\InvalidConfigException;  

    class MyComponent extends Component
    {
     public function welcome()
     {
      echo "Hello..Welcome to MyComponent";
     }  

    }

2、在config/web.php加入你的components

‘components‘ => [  

         ‘mycomponent‘ => [  

            ‘class‘ => ‘app\components\MyComponent‘,  

            ],
           ] 

3、在controllers/TestController.php中用你的components

namespace app\controllers;
use Yii;  

class TestController extends \yii\web\Controller
{
    public function actionWelcome()
    {
       Yii::$app->mycomponent->welcome();
    }  

} 

原文地址:http://blog.csdn.net/xiaoyangxiaodong/article/details/45078303

时间: 2024-12-26 07:09:06

[moka同学笔记]Yii2 自定义class、自定义全局函数(摘录)的相关文章

[moka同学笔记]yii2.0小物件的简单使用(第二种方法)

1.在widgets/TestWidget.php中 1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: moka同学 5 * Date: 2016/08/05 6 * Time: 18:15 7 */ 8 namespace app\widgets; 9 10 use yii\base\Widget; 11 12 class TestWidget extends Widget 13 { 14 public $msg = ''; 15 16 //初始

[moka同学笔记]yii2场景的使用(摘录)

前半部分为自己使用的过程,下边为转载的,具体地址见:http://blog.sina.com.cn/s/blog_88a65c1b0101j717.html 1.在model中 public function rules() { return [ [['join_verify', 'create_activity', 'is_open_group', 'is_open_child_com','sendmail_limit','sendmail_from_name','sendmail_from'

[moka同学笔记]yii2.0的下拉菜单与bootstrap下拉菜单

1.yii2下拉菜单 1 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">管理<b class="caret"></b></a> 2 <ul class="dropdown-menu"> 3 <li&

[moka同学笔记]Yii2.0给一张表中增加一个属性

1.model中建立关联 public function getUser(){ return$this->hasOne(User::className(),['id'=>'uid']) ; } 2.在视图中 <?= GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ // ['class' => 'yii\grid\SerialColumn'], 取消掉编号 ['attribute'=&g

[moka同学笔记]yii2.0 dropdownlist的简单使用

1.controller控制中 $modelCountrytelCode = CountryTelCode::find()->orderBy('id DESC')->all(); $telCode = ArrayHelper::map($modelCountrytelCode,'area_code','name'); //显示name,传值area_code ***********其他代码************ return $this->render('createUser', [

[moka同学笔记]redis练习Demo

<?php /** * Created by PhpStorm. * User: moka同学 * Date: 2017/3/13 * Time: 10:54 */ echo 'Hello redis'.'<br>'; $redis = new Redis(); $redis->connect("127.0.0.1","6379"); //demo1 /*$redis->set("name","This i

[moka同学笔记]三、Yii2.0课程笔记(魏曦老师教程)

关联字段增加搜索 post表关联adminuser表,通过post.author_id  adminuser.id关联,在YII2.0生成搜索,关联字段搜索时,需要输入关联字段author的id才能搜索,这不符合我们的要求,我们要求输入作者名就可以搜索出来,该作者对应的post.

[moka同学笔记]WINDOWS中cmd的切换目录cd命令失效

症状: 解决办法: 二.解决问题 原因是没有切换盘符步骤一:C:\Users\Administrator>D:步骤二:D:\>

[moka同学笔记]window下.htacess文件 与linux下.htacess文件

windows下 # Turn on URL rewritingRewriteEngine On # Installation directoryRewriteBase / # Protect hidden files from being viewed<Files .*>    Order Deny,Allow    Deny From All</Files> # Protect application and system files from being viewedRewr