Phalcon - Designing the Login Page


Advertisements

UsersController.php

<?php  
class UsersController extends Phalcon\Mvc\Controller {  
   public function indexAction() {  
   }  
   public function loginAction() {  
      if ($this->request->isPost()) { 
         $user = Users::findFirst(array( 
            'login = :login: and password = :password:', 'bind' => array( 
               'login' => $this->request->getPost("login"), 
               'password' => $this->request->getPost("password") 
            ) 
         ));  
         if ($user === false) { 
            $this->flash->error("Incorrect credentials"); 
            return $this->dispatcher->forward(array( 
               'controller' => 'users', 'action' => 'index' 
            )); 
         } 
         $this->session->set('auth', $user->id);  
         $this->flash->success("You've been successfully logged in"); 
      } 
      return $this->dispatcher->forward(array( 
         'controller' => 'posts', 'action' => 'index' 
      )); 
   }  
   public function logoutAction() { 
      $this->session->remove('auth'); 
      return $this->dispatcher->forward(array( 
         'controller' => 'posts', 'action' => 'index' 
      )); 
   } 
}

The UsersController includes functionality with log in and log out features. It checks for the associated value in the records for “Users” table. If the value gets authenticated, the user successfully logs in or else gets an error message.

Following is the output of the above code.

UsersController

Once logged in to the web application, the output will look as shown in the following screenshot.

Screenshot

We will look at implementing views in the next chapter which will focus on categories and posts management.

phalcon_scaffolding_application.htm
Advertisements