2010-07-22 1 views
6

У меня есть отдельный класс, который я написал в PHP для некоторых основных функций LDAP/AD. и я хотел бы использовать этот класс в проекте, над которым я работаю в cakephp.Как использовать автономный класс в cakephp 1.3?

Похоже, что в cakephp 1.2 я мог бы просто добавить класс в качестве поставщика, но похоже, что cakephp 1.3 удалил поддержку для поставщиков. Итак, как я могу назвать несколько функций из этого класса?

(Я хотел бы, чтобы попытаться сохранить сам класс такой же, и не превратить его в плагин, так как это кажется ненужным)

Спасибо!

код ниже:

**<?php 
class UsersController extends AppController { 

var $name = 'Users'; 

    //commented out because it breaks the script 
    //App::import('Lib', 'ldap'); 


function index() { 
    $this->User->recursive = 0; 
    $this->set('users', $this->paginate()); 
} 

    function login() { 

     if (!empty($this->data)) { 
       if ($ldap->auth($this->data['User']['user'],$this->data['User']['password'])) { 
         $this->Session->setFlash(__('The user has been saved', true)); 
         $this->Session->write('user', $this->data['User']['user']); 
         $this->redirect(array('action' => 'index')); 
       } else { 
         $this->Session->setFlash(__('Login Failed', true)); 
       } 
     } 
    } 

    function logout() { 
     $this->Session->delete('user'); 
     $this->redirect($this->referer()); 

    } 

function view($id = null) { 
    if (!$id) { 
     $this->Session->setFlash(__('Invalid user', true)); 
     $this->redirect(array('action' => 'index')); 
    } 
    $this->set('user', $this->User->read(null, $id)); 
} 

function add() { 
    if (!empty($this->data)) { 
     $this->User->create(); 
     if ($this->User->save($this->data)) { 
      $this->Session->setFlash(__('The user has been saved', true)); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The user could not be saved. Please, try again.', true)); 
     } 
    } 
    $projects = $this->User->Project->find('list'); 
    $this->set(compact('projects')); 
} 

function edit($id = null) { 
    if (!$id && empty($this->data)) { 
     $this->Session->setFlash(__('Invalid user', true)); 
     $this->redirect(array('action' => 'index')); 
    } 
    if (!empty($this->data)) { 
     if ($this->User->save($this->data)) { 
      $this->Session->setFlash(__('The user has been saved', true)); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The user could not be saved. Please, try again.', true)); 
     } 
    } 
    if (empty($this->data)) { 
     $this->data = $this->User->read(null, $id); 
    } 
    $projects = $this->User->Project->find('list'); 
    $this->set(compact('projects')); 
} 

function delete($id = null) { 
    if (!$id) { 
     $this->Session->setFlash(__('Invalid id for user', true)); 
     $this->redirect(array('action'=>'index')); 
    } 
    if ($this->User->delete($id)) { 
     $this->Session->setFlash(__('User deleted', true)); 
     $this->redirect(array('action'=>'index')); 
    } 
    $this->Session->setFlash(__('User was not deleted', true)); 
    $this->redirect(array('action' => 'index')); 
} 
} 
?>** 

ответ

7

Cake 1,3 до сих пор прекрасно поддерживает идею файлов поставщика. Кроме того, они now also support "libraries", дополнительные классы, которые не являются сторонними классами. Просто поместите свои файлы в каталог /vendors или /libs и загрузите файл, используя App::import.

+0

Я положил ldap.php в папку ЛИЭС и поставить "App :: импорт ('Lib', 'Ldap');" в моем users_controller.php, но когда я вызываю любую страницу, я получаю «Ошибка синтаксиса: синтаксическая ошибка, неожиданный T_STRING, ожидающий T_FUNCTION в C: \ xampp \ htdocs \ timetracker \ controllers \ users_controller.php в строке 6» – lanrat

+2

@mrlanrat Ну, у вас есть ** синтаксическая ошибка ** где-то, что не имеет ничего общего с Cake's libs или vendors. – deceze

+0

отправьте код, и мы сможем помочь! – Leo

2

Я получил его работу, мне пришлось вызвать «App :: import (« Lib »,« ldap »); вне класса контроллера, а затем вызывать его как новый класс внутри функции, которую я хотел.

Ниже конечный результат

<?php 
App::import('Lib', 'ldap'); 
class UsersController extends AppController { 

    var $name = 'Users'; 

    function index() { 
     $this->User->recursive = 0; 
     $this->set('users', $this->paginate()); 
    } 

     function login() { 

      if (!empty($this->data)) { 
       $ldap = new ldap; 
        if ($ldap->auth($this->data['User']['user'],$this->data['User']['password'])) { 

          if (!$this->User->findByUser($this->data['User']['user'])) 
          { 
           $ldap_info = $ldap->getInfo($this->data['User']['user']); 
           $this->data['User']['name'] = $ldap_info['name']; 
           $this->add(); 
          } 

          $this->Session->write('user', $this->data['User']['user']); 
          $this->redirect(array('action' => 'index')); 
        } else { 
          $this->Session->setFlash(__('Login Failed', true)); 
        } 
      } 
     } 

     function logout() { 
      $this->Session->delete('user'); 
      $this->redirect($this->referer()); 

     } 

    function view($id = null) { 
     if (!$id) { 
      $this->Session->setFlash(__('Invalid user', true)); 
      $this->redirect(array('action' => 'index')); 
     } 
     $this->set('user', $this->User->read(null, $id)); 
    } 

    private function add() { 
     if (!empty($this->data)) { 
      $this->User->create(); 
      if ($this->User->save($this->data)) { 
       $this->Session->setFlash(__('The user has been saved', true)); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please, try again.', true)); 
      } 
     } 
     $projects = $this->User->Project->find('list'); 
     $this->set(compact('projects')); 
    } 

    function edit($id = null) { 
     if (!$id && empty($this->data)) { 
      $this->Session->setFlash(__('Invalid user', true)); 
      $this->redirect(array('action' => 'index')); 
     } 
     if (!empty($this->data)) { 
      if ($this->User->save($this->data)) { 
       $this->Session->setFlash(__('The user has been saved', true)); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please, try again.', true)); 
      } 
     } 
     if (empty($this->data)) { 
      $this->data = $this->User->read(null, $id); 
     } 
     $projects = $this->User->Project->find('list'); 
     $this->set(compact('projects')); 
    } 

    function delete($id = null) { 
     if (!$id) { 
      $this->Session->setFlash(__('Invalid id for user', true)); 
      $this->redirect(array('action'=>'index')); 
     } 
     if ($this->User->delete($id)) { 
      $this->Session->setFlash(__('User deleted', true)); 
      $this->redirect(array('action'=>'index')); 
     } 
     $this->Session->setFlash(__('User was not deleted', true)); 
     $this->redirect(array('action' => 'index')); 
    } 
} 
?>