V
V
Viktor Yanyshev2016-12-13 09:55:49
PHP
Viktor Yanyshev, 2016-12-13 09:55:49

Why is Notice: Undefined index: ipphone in MVC, but everything works without MVC?

When loading a page at a tender address, users are loaded from Active Directory. If you take the Sprav class and place it together with the view ( I mean that all the code is in 1 file and I get access to it at localhost.dev/sprav.php ), then all users will be unloaded and there are no problems. If I parse the code on MVC, then when you go to the page with the list of users, the page starts loading constantly and throwing out an error about an unknown array index (there are all keys (index) of the array!) received from AD.
5d6a11985adb4a8db6c3805e3ca2d07d.png
And then spins the data non-stop $data
Model class (Sprav.php):

class Sprav {

    public static function getUserLdap() {

        $ldapUser = "[email protected]";
        $ldapPass = "PASSWoRD";
        $ldapHost = "DOMAIN";
        $ldapPort = '389';
        $dn = "OU=MAIN,DC=LOCAL,DC=DOMAIN,DC=RU";

        $filter="(&(primaryGroupID=513))";

        $justthese = array("ou", "sn", "givenname","department", "samaccountname", "mail","ipphone","mobile","title","useraccountcontrol","company");

        $connect = ldap_connect($ldapHost, $ldapPort);

        ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);

        $data = [];

        if ($connect) {

            $bind = ldap_bind($connect,$ldapUser,$ldapPass);

            if ($bind) {
                $result = ldap_search($connect,$dn,$filter,$justthese);
                $data = ldap_get_entries($connect,$result);

            } else {
                echo "LDAP-Авторизация не удалась...<br>";
            }

        } else {
            echo "LDAP-соединение не удалась...<br>";
        }

        $users = [];

        for ($i = 0; count($data) > $i; $i++) {
            $users[$i]['lastname'] = $data[$i]['sn'][0];
            $users[$i]['name'] = $data[$i]['givenname'][0];
            $users[$i]['department'] = $data[$i]['department'][0];
            $users[$i]['login'] = $data[$i]['samaccountname'][0];
            $users[$i]['mail'] = $data[$i]['mail'][0];
            $users[$i]['extension'] = $data[$i]['ipphone'][0];
            $users[$i]['mobile'] = $data[$i]['mobile'][0];
            $users[$i]['post'] = $data[$i]['title'][0];
            $users[$i]['code'] = $data[$i]['useraccountcontrol'][0];
            $users[$i]['company'] = $data[$i]['company'][0];
        }

        return $users;

    }
}

Controller (SpravController):
require_once(ROOT.'/model/Sprav.php');

class SpravController
{

    public function actionView() {

        $user = Sprav::getUserLdap();

        require_once(ROOT.'/view/sprav.php');
        return true;
    }

}

Template code(/view/sprav.php):
<? for($i=0; count($user) > $i; $i++): ?>
            <tr>
                <td><?= $i; ?>/<?= $user[$i]['code']; ?></td>
                <td><?= $user[$i]['lastname']; ?></td>
                <td><?= $user[$i]['name']; ?></td>
                <td><?= $user[$i]['company']; ?></td>
                <td><?= $user[$i]['department']; ?></td>
                <td><?= $user[$i]['post']; ?></td>
                <td><?= $user[$i]['extension']; ?></td>
                <td><?= $user[$i]['mobile']; ?></td>
                <td><?= $user[$i]['mail']; ?></td>
            </tr>
    <? endfor; ?>

$data
array (size=51)
  'count' => int 50
  0 => 
    array (size=22)
      'sn' => 
        array (size=2)
          'count' => int 1
          0 => string 'Фамилия' (length=12)
      0 => string 'sn' (length=2)
      'title' => 
        array (size=2)
          'count' => int 1
          0 => string 'Менеджер по продажам' (length=38)
      1 => string 'title' (length=5)
      'givenname' => 
        array (size=2)
          'count' => int 1
          0 => string 'Имя юзера' (length=14)
      2 => string 'givenname' (length=9)
      'department' => 
        array (size=2)
          'count' => int 1
          0 => string 'Продажи' (length=14)
      3 => string 'department' (length=10)
      'company' => 
        array (size=2)
          'count' => int 1
          0 => string 'Название Компании' (length=27)
      4 => string 'company' (length=7)
      'useraccountcontrol' => 
        array (size=2)
          'count' => int 1
          0 => string '512' (length=3)
      5 => string 'useraccountcontrol' (length=18)
      'samaccountname' => 
        array (size=2)
          'count' => int 1
          0 => string 'В транслите' (length=8)
      6 => string 'samaccountname' (length=14)
      'ipphone' => 
        array (size=2)
          'count' => int 1
          0 => string '110' (length=3)
      7 => string 'ipphone' (length=7)
      'mail' => 
        array (size=2)
          'count' => int 1
          0 => string '[email protected]' (length=18)
      8 => string 'mail' (length=4)
      'mobile' => 
        array (size=2)
          'count' => int 1
          0 => string '+7 777 777 88 99' (length=18)
      9 => string 'mobile' (length=6)
      'count' => int 10
      'dn' => string 'dn-path' (length=76)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OnYourLips, 2016-12-13
@OnYourLips

The error says that there is no index, but you are trying to get it.
Check the index before creating.
Better yet, create a DTO and don't use associative arrays when passing data from one place to another: create a User class and turn your $data into an array of type User as soon as you get it.
And finally, I will say that composer will make your life much more convenient.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question