Foros del Web » Programando para Internet » PHP » Zend »

[SOLUCIONADO] autenticación con zend

Estas en el tema de autenticación con zend en el foro de Zend en Foros del Web. Hola: Me está costando aprender zend, deseo realizar autenticación de usuario y el error que tengo es este Cita: Catchable fatal error: Argument 1 passed ...
  #1 (permalink)  
Antiguo 25/01/2013, 08:12
 
Fecha de Ingreso: enero-2010
Mensajes: 491
Antigüedad: 14 años, 2 meses
Puntos: 12
autenticación con zend

Hola:

Me está costando aprender zend, deseo realizar autenticación de usuario y el error que tengo es este

Cita:
Catchable fatal error: Argument 1 passed to Zend_Auth::authenticate() must be an instance of Zend_Auth_Adapter_Interface, null given, called in C:\xampp\htdocs\biblioteca\application\controllers \LoginController.php on line 62 and defined in C:\xampp\library\Zend\Auth.php on line 115
mi controlador es este
Código PHP:
Ver original
  1. class LoginController extends Zend_Controller_Action {
  2.    
  3.     public function init() {
  4.         /* Initialize action controller here */
  5.     }
  6.    
  7.     public function indexAction() {
  8.         $this->view->form = $this->getForm ();
  9.         // $this->view->form = $this->loginAction();
  10.     }
  11.     public function getForm() {
  12.         return new Application_Form_LoginForm ( array (
  13.                 'action' => '/login/process',
  14.                 'method' => 'post'
  15.         ) );
  16.     }
  17.    
  18.     public function getAuthAdapter(array $params) {
  19.     }
  20.    
  21.     public function preDispatch() {
  22.         if (Zend_Auth::getInstance ()->hasIdentity ()) {
  23.             // If the user is logged in, we don't want to show the login form;
  24.             // however, the logout action should still be available
  25.             if ('logout' != $this->getRequest ()->getActionName ()) {
  26.                 $this->_helper->redirector ( 'index', 'index' );
  27.             }
  28.         } else {
  29.             // If they aren't, they can't logout, so that action should
  30.             // redirect to the login form
  31.             if ('logout' == $this->getRequest ()->getActionName ()) {
  32.                 $this->_helper->redirector ( 'index' );
  33.             }
  34.         }
  35.     }
  36.     public function processAction() {
  37.         $request = $this->getRequest ();
  38.        
  39.         // Check if we have a POST request
  40.         if (! $request->isPost ()) {
  41.             return $this->_helper->redirector ( 'index' );
  42.         }
  43.        
  44.         // Get our form and validate it
  45.         $form = $this->getForm ();
  46.         if (! $form->isValid ( $request->getPost () )) {
  47.             // Invalid entries
  48.             $this->view->form = $form;
  49.             return $this->render ( 'index' ); // re-render the login form
  50.         }
  51.        
  52.         // Get our authentication adapter and check credentials    
  53.         $adapter = $this->getAuthAdapter ( $form->getValues () );
  54.         $auth = Zend_Auth::getInstance ();
  55.         $result = $auth->authenticate ( $adapter );
  56.             // /
  57.         $values = $form->getValues ();
  58.         $adapter = new Zend_Auth_Adapter_DbTable ( $this->db );
  59.         $adapter->setIdentity ( $values ['username'] )->setCredential ( $values ['password'] );
  60.         $auth = Zend_Auth::getInstance ();
  61.         $result = $auth->authenticate ( $adapter );    
  62.         ///
  63.         if (! $result->isValid ()) {
  64.             // Invalid credentials
  65.             $form->setDescription ( 'Invalid credentials provided' );
  66.             $this->view->form = $form;
  67.             return $this->render ( 'index' ); // re-render the login form
  68.         }
  69.        
  70.         // We're authenticated! Redirect to the home page
  71.         $this->_helper->redirector ( 'index', 'index' );
  72.     }
  73.     public function logoutAction() {
  74.         Zend_Auth::getInstance ()->clearIdentity ();
  75.         $this->_helper->redirector ( 'index' ); // back to login page
  76.     }
  77. }

por favor quisiera me expliquen el problema
  #2 (permalink)  
Antiguo 25/01/2013, 08:18
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 2 meses
Puntos: 845
Respuesta: autenticación con zend

El mensaje de error te lo dice todo, no es un tema Zend, es OOP básico, Zend_Auth::authenticate espera una instancia de Zend_Auth_Adapter_Interface y tu le estas pasando null, verifica tu código, acaso no falta gente en LoginController::getAuthAdapter ?

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)
  #3 (permalink)  
Antiguo 25/01/2013, 08:41
 
Fecha de Ingreso: enero-2010
Mensajes: 491
Antigüedad: 14 años, 2 meses
Puntos: 12
Respuesta: autenticación con zend

Gracias por tu pronta respuesta, pero como dije quisiear entender el problema, tengo un:
Código PHP:
Ver original
  1. public function indexAction() {
  2.     $this->view->form = $this->getForm ();
  3. }
que llama a:
Código PHP:
Ver original
  1. public function getForm() {
  2.         return new Application_Form_LoginForm ( array (
  3.                 'action' => '/login/process',
  4.                 'method' => 'post'
  5.         ) );
  6.     }
donde LoginForm tiene los text de usuario y contraseña y un boton de ingresar, en el array 'action'=>'/login/proccess' le dice que?
que busque un método proccess pues el único que tengo en el controlador es public function processAction(), de ser así, el código sería:
Código PHP:
Ver original
  1. public function processAction() {
  2.         $request = $this->getRequest ();
  3.        
  4.         if (! $request->isPost ()) {
  5.             return $this->_helper->redirector ( 'index' );
  6.         }
  7.        
  8.         $form = $this->getForm ();
  9.         if (! $form->isValid ( $request->getPost () )) {
  10.             $this->view->form = $form;
  11.             return $this->render ( 'index' ); // re-render the login form
  12.         }
  13.        
  14.         $adapter = $this->getAuthAdapter ( $form->getValues () );
  15.         $auth = Zend_Auth::getInstance ();
  16.         $result = $auth->authenticate ( $adapter );
  17.         $values = $form->getValues ();
  18.         $adapter = new Zend_Auth_Adapter_DbTable ( $this->db );
  19.         $adapter->setIdentity ( $values ['username'] )->setCredential ( $values ['password'] );
  20.         $auth = Zend_Auth::getInstance ();
  21.         $result = $auth->authenticate ( $adapter );    
  22.         if (! $result->isValid ()) {
  23.             $form->setDescription ( 'Invalid credentials provided' );
  24.             $this->view->form = $form;
  25.             return $this->render ( 'index' ); // re-render the login form
  26.         }
  27.        
  28.         $this->_helper->redirector ( 'index', 'index' );
  29.     }
  30.     public function logoutAction() {
  31.         Zend_Auth::getInstance ()->clearIdentity ();
  32.         $this->_helper->redirector ( 'index' ); // back to login page
  33.     }
  34. }
entiendo que con $request = $this->getRequest (); recupera los datos enviados con el boton submit 'ingresar', y con
Código PHP:
Ver original
  1. if (! $request->isPost ()) {
  2.             return $this->_helper->redirector ( 'index' );
  3.         }
si no fue enviado con un método post usa un helper redirigiendolo nuevamente a index que en mi caso al no estár autenticado trabaja el predispatch y nuevamente se va a la autenticación.

en mi ide el error me sale en la liena 62 que es
Código PHP:
Ver original
  1. $auth = Zend_Auth::getInstance ();
que parámetros debo pasarle?

muchas gracias .
  #4 (permalink)  
Antiguo 25/01/2013, 09:06
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 2 meses
Puntos: 845
Respuesta: autenticación con zend

Es que no se bien como dejarlo mas claro wilmer30, el método authenticate de Zend_Auth necesita una instancia de Zend_Auth_Adapter_Interface y tu le estas pasando un null, esto es porque tu método getAuthAdapter en LoginController retorna null cuando debería retornar el adapter, osea, tu método getAuthAdapter debería tener algo asi:

Código PHP:
Ver original
  1. $dbAdapter   = Zend_Db_Table::getDefaultAdapter();
  2. $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
  3. $authAdapter->setTableName('users')
  4.             ->setIdentityColumn('username')
  5.             ->setCredentialColumn('password')
  6. return $authAdapter;

o bien algún otro adapter, se entiende ?
__________________
http://es.phptherightway.com/
thats us riders :)
  #5 (permalink)  
Antiguo 04/02/2013, 07:48
 
Fecha de Ingreso: enero-2010
Mensajes: 491
Antigüedad: 14 años, 2 meses
Puntos: 12
Respuesta: autenticación con zend

Gracias, este tema fue solucionado

Etiquetas: autenticación, frameworks-y-php-orientado-a-objetos
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:20.