While working on one of the projects realized how is to created fully functional login form using CakePHP framework. For people who are not familiar with it please visit : http://cakephp.org/ for more information. I am also thinking about writing a small tutorial on how to set this framework up, but it will be later.
So let’s assume you already have CakePHP set up and all you have your ‘user’ table in the database.
First of all lets create login.ctp :
<?php if ($session->check(‘Message.auth’)) $session->flash(‘auth’); // If authentication message it exist it will be displayed echo $form->create(‘User’, array(‘action’ => ‘login’)); // creating user form with 2 fields email and password echo $form->input(’email’); echo $form->input(‘password’); echo $form->end(‘Login’); ?>
Now lets edit our user_controller.php (we don’t need to edit much because CakePHP is taking care of everything):
function logout() { $this->redirect($this->Auth->logout()); } function beforeFilter() { //function that enables people to register without logging in $this->Auth->allow(‘add’); parent::beforeFilter(); }
And the last piece is AppController .
Edit app_controller.php :
class AppController extends Controller { var $components = array(‘Auth’); function beforeFilter() { $this->Auth->userModel = ‘User’; $this->Auth->fields = array(‘username’ => ’email’, ‘password’ => ‘password’); $this->Auth->loginAction = array(‘admin’ => false, ‘controller’ => ‘users’, ‘action’ => ‘login’); $this->Auth->loginRedirect = array(‘controller’ => ‘users’, ‘action’ => ‘index’); } }
There is not much to say about the code above, to my mind it is very simple to read.
And that is it. This is a benefit of developing on the existing framework. Such things as login is already included, all you need is just to properly call it.
Thanks for installing the Bottom of every post plugin by Corey Salzano. Contact me if you need custom WordPress plugins or website design.
getting error UsersController1Controller some bodyplease help
this code gives me fatal error:
Error: Call to a member function check() on a non-object
File: C:wampwwwcakeappViewUserslogin.ctp
Line: 3
I wanna know how to insert data from a form to MySQL with cakephp
I’ll right away take hold of your rss feed as I can not to find your e-mail subscription hyperlink or e-newsletter service. Do you’ve any? Please allow me recognise in order that I could subscribe. Thanks.
[…] CakePHP: Creating Login form | My Programming Blog – While working on one of the projects realized how is to created fully functional login form using CakePHP framework. For people who are not familiar with it please … […]
Can you tell me User Login from (same topic) in cakephp 3.2 And How we get user forget password in cakephp 3.2 ?please reply me…..
Hey Mandeep, thank you for your comment.
Let me clarify, are you asking: How to setup Login Form and Forgot your password functionality in CakePHP 3.2 ?