2010-07-22 11 views
6

Tengo una clase independiente que escribí en PHP para algunas funciones LDAP/AD muy básicas. y me gustaría utilizar esta clase en un proyecto en el que estoy trabajando en cakephp.¿Cómo uso una clase independiente en cakephp 1.3?

Parece que en cakephp 1.2 solo podría agregar la clase como proveedor, sin embargo, parece que cakephp 1.3 eliminó la compatibilidad con los proveedores. Entonces, ¿cómo voy a llamar a algunas funciones de esta clase?

(me gustaría tratar de mantener la clase en sí mismo y no convertirlo en un plugin, ya que parece innecesario)

Gracias!

código de abajo:

**<?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')); 
} 
} 
?>** 

Respuesta

7

Cake 1.3 todavía apoya perfectamente la idea de archivos de proveedores. Además, ellos now also support "libraries", clases adicionales que no son clases de terceros. Solo inserte sus archivos en el directorio /vendors o /libs y cargue el archivo usando App::import.

+0

puse mi ldap.php en la carpeta libs y puse "App :: import ('lib', 'LDAP');" en mi users_controller.php, pero cuando llamo a cualquier página obtengo "Parse error: error de sintaxis, inesperado T_STRING, esperando T_FUNCTION en C: \ xampp \ htdocs \ timetracker \ controllers \ users_controller.php en la línea 6" – lanrat

+2

@mrlanrat Bueno, tiene un ** error de sintaxis ** en algún lado, que no tiene nada que ver con las librerías o proveedores de Cake. – deceze

+0

publique el código y podremos ayudarlo. – Leo

2

Lo tengo funcionando, tuve que llamar a "App :: import ('Lib', 'ldap');" fuera de la clase de controlador y luego llamarlo como una nueva clase dentro de la función que quería.

a continuación es el resultado final

<?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')); 
    } 
} 
?> 
Cuestiones relacionadas