How to write global functions in Yii2 and access t

2019-04-23 23:35发布

Yii1.1 had a CComponent class that had a CBaseController which was the base class for CController. There was a /protected/components/Controller.php class which enabled any function in that class to be accessed in any view.

Yii2 no longer possess the CComponent class. The Yii2 guide indicates that "Yii 2.0 breaks the CComponent class in 1.1 into two classes: yii\base\Object and yii\base\Component". Does anyone know how to write global functions in Yii2 and them in any view, just like it was in Yii1.1 using /protected/components/Controller.php?

A couple of similar topics discuss custom answers, but I would like to know whether there is an official way of doing that, not the custom way.

8条回答
Viruses.
2楼-- · 2019-04-24 00:05

Follow Step:
1) create following directory "backend/components"
2) create "BackendController.php" controller in "components" folder

<?php    
    namespace backend\components;
    use Yii;

    class BackendController extends \yii\web\Controller
    {
        public function init(){
            parent::init();

        }
        public function Hello(){
            return "Hello Yii2";
        }
    }

3) all backed controller extend to "BackendController" (like).

<?php
namespace backend\controllers;

use Yii;
use backend\components\BackendController;

class SiteController extends BackendController
{

    public function beforeAction($event)
    {
        return parent::beforeAction($event);
    }

    public function actionIndex(){
        /* You have to able for call hello function to any action and view */
        //$this->Hello(); exit;

        return $this->render('index');
    }
}

4) create your action view page "index.php" (like)

<?php
print $this->Hello();
?>
查看更多
祖国的老花朵
3楼-- · 2019-04-24 00:06

I am new to Yii and tried this as solution for example messages that are shown in layout i created a baseController in frontend/components (all controllers extends this BaseController) and do the always needed stuff in the init section. and set the data to the view/layout with $this->view->params in view or layout use the data with $this->params i dont know if this is the best practice .. sorry for my bad english, i hope it helps

 namespace frontend\components;
    use Yii;
    use frontend\models\UserMessages;

    class BaseController extends \yii\web\Controller
    {
        public function init(){
            parent::init();

            // user nachrichten
            if (!Yii::$app->user->isGuest) {
                    $messages = UserMessages::find()
                    ->where(['user_recipient_id' => Yii::$app->user->identity->id])
                    ->orderBy('date_created')
                    ->all();
                    $this->view->params['userMessages'] = $messages;
            }
        }
    }
查看更多
我想做一个坏孩纸
4楼-- · 2019-04-24 00:11

One option is to create a file (probably in your components directory) that has your functions in it:

function myFunction($parameters)
{
    // Do stuff and return stuff
}

and then include it in your index.php file:

require_once(__DIR__ . "../components/MyGlobalFunctions.php');

The disadvantage is that this is now outside the OOP scope of the application. I used this method to create a dd() (dump and die) function like Laravel has.

查看更多
萌系小妹纸
5楼-- · 2019-04-24 00:14

As, question is already answered. But, still I would like to answer it for future uses.

I'm using yii2-app-basic. [NOTE : Directory structure may differ for yii2-app-advanced.]

My Directory Structure:

->Root Folder
    ->assets
    ->commands
    .
    .
    ->controllers
        ->SiteController.php
        ->CommonController.php (New common controller created)
    ->mail
    .
    .
    ->modules
        ->users
            ->controllers
                ->UsersController.php
            ->models
                ->Users.php
            ->views
                ->index.php
            ->Users.php
    .
    .
    .

CommonController.php [Common controller(see in directory structure) which will be extended in all controller, if needed.]

<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;

class CommonController extends Controller
{
    .
    .
    // I used this function for checking login access of user
    public function checkLoginAccess() {
        //Write your own custom code
        // For example
        $loginAccess = "No";
        if($loginAccess == "No") {
            Yii::$app->user->logout();
        }
    }
}

UsersController.php

<?php
namespace app\modules\users\controllers;

use Yii;
.
.
use app\controllers\CommonController;

//Extend CommonController 
class UsersController extends CommonController 
{
  // Inside init() function, use checkLoginAccess() method to be called
  public function init() {
    parent::init();
    if(!Yii::$app->user->isGuest) {
      $this->checkLoginAccess();
    }
  }
    .
    .
    .
}

So, Whenever this UsersController.php controller will come into an account, first init() method will be called, And in init() method CommonController method checkLoginAccess() will get call automatically. So, whenever a member having no login acess, he/she will be automatically logged out.

Hope it will help. Any Problem/Question, feel free to ask.

查看更多
做个烂人
6楼-- · 2019-04-24 00:14

Easily you can create a component:

<?php

namespace frontend\components;

use yii\base\Component;

class MyComponent extends Component
{
    public function hello()
    {
        return "Hello, World!";
    }
}

Register it as a component in your frontend/config/main.php file:

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    // Some code goes here
    'components' => [
        // some code here
        'memem' => [
            'class' => 'frontend\components\MyComponent'
        ],
    ],
];

and finally use it where you need:

<?php

/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use yii\widgets\Breadcrumbs;
?>
<?php $this->beginPage()?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="<?=Yii::$app->charset?>">
  <meta name="usage" value="<?=Yii::$app->memem->hello()?>">
查看更多
smile是对你的礼貌
7楼-- · 2019-04-24 00:17

You can also make components in YII2

Create a directory and a file same as in YII1 in frontend or backend and write below code in it

frontend/components/Controller.php

/**
 * Description of Controller
 *
 * @author mohit
 */

namespace frontend\components;
use Yii;
class Controller extends \yii\web\Controller{
    //put your code here

    public function beforeAction($action) {
        parent::beforeAction($action);
        if(true)
            return true;
        else
            return false;

    }
}

After that use it in every controller by extending this class like this-

namespace frontend\controllers;
use Yii;
use frontend\components\Controller;
class SomeController extends Controller
{
    // code
}
查看更多
登录 后发表回答