search

Home  >  Q&A  >  body text

When a specific IP is detected in Yii2, raise the corresponding event

I'm developing a system for a restaurant and every time someone connects to the restaurant's network, a menu tab must pop up. I tried this by detecting the user's IP address and seeing if it matched the restaurant's network. But it doesn't work at all. (All codes must use Yii2, this is my first contact, no experience)

I checked all the folder and file names but they are all correct and the event is not running at all.

Just to provide some background information, I have a component that does the instrumentation work, and all the code is run in that file. The problem seems to be in the actual controller.

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;



class NetworkController extends Controller{

    public function actionNetworkTeste(){
        
        Yii::$app->networkEventManager->on('specificNetworkConnectionDetected', [$this, 'onSpecificNetworkConnectionDetected']);

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

    public function onSpecificNetworkConnectionDetected($event){

        //Yii::debug('Funcionou');

        return $this->render('network-alt'); 

        
    }
}


?>

I don't even know if this is the right way to do it, popping up a tab when someone is connected to the network, but it's better than not trying at all. I would really appreciate some help as I have no experience with Yii2.

P粉031492081P粉031492081529 days ago739

reply all(1)I'll reply

  • P粉276577460

    P粉2765774602023-09-11 20:36:24

    You should read the section on Handling requests

    on the Yii2 documentation

    To get the client's IP, the document is here, and the request API document is here

    $userHost = Yii::$app->request->userHost;
    $userIP = Yii::$app->request->userIP;

    reply
    0
  • Cancelreply