Simple registration in CakePHP

Suppose that there is a users table in recipe_db database.

users

In users table we will save a user data using cakephp. Let create a controller name usersController :


class UsersController extends AppController{</pre>
public function add(){
 if($this->request->is('post')){
 if($this->User->find('all',array('conditions'=>"email='{$this->data['User']['email']}'"))){
 $this->Session->setFlash('Email Address is already used,Plesae try again');
 }
 else{
 $this->request->data['User']['id']= uniqid();
 if($this->User->save($this->request->data)) {
 $this->Session->setFlash('Successully save your information!!');
 }
 else{
 $this->Session->setFlash('Unable to save your Information!!');
 }
 }
 }
 }

}

this add() of  UsersController class will insert users information into db table users. Corresponding view of this add action of users controller will be into a Users folder , and name it as add.ctp .


<?php echo $this->Form->create(); ?>
<fieldset>
 <legend>Add New User</legend>
 <?php
 echo $this->Form->input('name',array('required'=>'required'));
 echo $this->Form->input('email',array('required'=>'required','type'=>'email'));
 echo $this->Form->input('country',array('options'=>array(
 'bangladesh'=>'Bangladesh',
 'india'=>'India',
 'pakistan'=>'Pakistan' ,
 'usa'=>'USA',
 'uk'=>'UK',
 'canada'=>'Canada'
 )
 ));
 echo $this->Form->input('password',array('required'=>'required'));
 echo $this->Form->input('Confirm password',array('type'=>'password'));
 echo $this->Form->end('Save');
 ?>
</fieldset>

To validate user input we will add some validation script in User model.

class User extends AppModel{
 //put your code here
 public $validate=array(
 'name'=>array(
 'rule'=>'notEmpty',
 'required'=>true,
 'message'=>'Input your name'
 ),
 'email'=>array(
 'email'=>array(
 'rule'=>'email',
 'message'=>'Input a valid emial address'
 ),
 'email'=>array(
 'rule'=>'notEmpty',
 'required'=>true,
 'message'=>'Input your email address'
 )

 ),

 'password'=>array(
 'alphaNumeric' => array(
 'rule' => 'alphaNumeric',
 'required' => true,
 'message' => 'Alphabets and numbers only'
 ),
 'password' => array(
 'rule' => array('minLength', '8'),
 'message' => 'Minimum 8 characters long'
 ),
 ),
 );

}

Advertisements

Software Engineer at The Jaxara IT Ltd , Dhaka , Bangladesh

Tagged with: , , ,
Posted in CakePHP
2 comments on “Simple registration in CakePHP
  1. sj says:

    really helpful..thankxxx

  2. mandeep says:

    please send me user login ,registration ,edit user information ,display user information,find user data from table in cakephp 3.2 with coding on my mail.please urgently required me my gmail is-mandeepkhaleriya.08@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: