2010-07-22 24 views
6

Tôi có một lớp độc lập mà tôi đã viết bằng PHP cho một số hàm LDAP/AD rất cơ bản. và tôi muốn sử dụng lớp này trong một dự án tôi đang làm việc trong cakephp.Làm thế nào để sử dụng một lớp độc lập trong CakePHP 1.3?

Có vẻ như trong cakephp 1.2 Tôi chỉ có thể thêm lớp này làm nhà cung cấp, tuy nhiên có vẻ như đã hỗ trợ loại bỏ cakephp 1.3 cho nhà cung cấp. Vì vậy, làm thế nào tôi sẽ đi về gọi một vài chức năng từ lớp này?

(Tôi muốn cố gắng giữ cho lớp đó giống nhau và không biến nó thành plugin, vì điều đó có vẻ không cần thiết)

Cảm ơn!

mã bên dưới:

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

Trả lời

7

Cake 1.3 vẫn hoàn toàn ủng hộ ý tưởng file nhà cung cấp. Ngoài ra, họ now also support "libraries", các lớp bổ sung không phải là các lớp bên thứ 3. Chỉ cần mở tệp của bạn vào thư mục /vendors hoặc /libs và tải tệp bằng cách sử dụng App::import.

+0

tôi đặt ldap.php của tôi trong thư mục libs và đặt "App :: nhập khẩu ('Lib', 'ldap');" trong user_controller.php của tôi, nhưng khi tôi gọi bất kỳ trang nào tôi nhận được "Lỗi phân tích cú pháp: lỗi cú pháp, T_STRING bất ngờ, mong T_FUNCTION trong C: \ xampp \ htdocs \ timetracker \ controllers \ users_controller.php trên dòng 6" – lanrat

+2

@mrlanrat Vâng, bạn có một lỗi cú pháp ** ** ở đâu đó, điều này không liên quan gì đến libs hoặc nhà cung cấp của Cake. – deceze

+0

đăng mã và chúng tôi có thể trợ giúp! – Leo

2

Tôi đã làm việc đó, tôi phải gọi "Ứng dụng :: nhập ('Lib', 'ldap');" bên ngoài lớp điều khiển và sau đó gọi nó là một lớp mới bên trong hàm tôi muốn.

Dưới đây là kết quả cuối cùng

<?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')); 
    } 
} 
?> 
Các vấn đề liên quan