Wednesday 29 January 2014

4 Steps To Make Registration Form In Cakephp



Before starting to learn about how to make registration form in Cakephp, I want to make it clear that you should read abo MVC model in Cakephp.

Step 1 - Create Table and fields

In first step we create table in database then create fields that are generally included in registration forms (like username, password, email id etc.)

Database query to create table: Create table ‘tablename’
Database query to create field:
CREATE TABLE tablename (
ID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID),
username VARCHAR (50) NOT Null,
password VARCHAR (50) NOT Null,
email VARCHAR (100) NOT Null,
firstname VARCHAR (25),
lastname VARCHAR (25),
)";
 

Note: - You can write any suitable name in place of “tablename”.

Step 2 - Create Model

Model is the part that takes care of the database logic. Create model with “tablename.php” in app/models as we defined name of table is “tablename” in first step. To make action on “tablename” we have to declare name (like name) because it uses in controller to save data. Code looks like: $this->name->save(data).

Code to create model: 
<?php
class tablename extends AppModel
{
var $name=' name ';
}
?>


Step 3 - Create View

View is the part that presents data received from model. Create view ‘register.ctp’ in app/view/tablename. We are creating form here in HTML although you can use helpers provided by cakephp to create form too.

Code to create view:
<html>
<form action="../tablename/register" method="post">
<p>Register your account.</p>
<label>Username:</label><input name="username" size="40" />
<label>Password:</label><input type="password" name="password" size="40"/>
<label>Email Id:</label><input name="email" size="40" maxlength="100" />
<label>First Name:</label><input name=" firstname " size="25" />
<label>Last Name:</label><input name=" lastname " size="25" />
<input type="submit" value="register" />
</form>
</html>


Note: Always remember the input name should be same as column name of our table.

Step 4 - Create Controller

Controller is a part that responds to user with combined action of model and view part. Create ‘tablename_controller.php‘ in app/controllers.

Code to create controller:
<?php
Class tablename_Controller extends AppController
{
function register(){
if (!empty($this->params['form']))
{
if($this->name->save($this->params['form']))
{
$this->flash('Registration Successful','/tablename/register’);
}
else
{
$this->flash('Not succeeded','/tablename/register');
}
}
}
}
?>


How above codes work?

First user open registration form here, "tablename/register/" in his browser. User fills the registration form details and click on register button. In controller it will check for the register function as we mentioned the action of form as ‘../register’ .

Register function in controller will save the data of form by using given statement
$this->name->save($this->params['form'])

This statement create insert query in database, i.e.
Insert INTO
Tablename(username, password, email, firstname, lastname)
Values
(‘mike’,’mike123’,’mike@gmail.com’,’mike’,’hussy’)


After insert command, a message pop-ups on your window “Registration successful” and you are done!

Resource: W3schools.com

Monday 6 January 2014

3 Ways to load models in Cakephp framework


There are three ways in CakePHP to load models. In these three methods, two works outside of a Controller.

App::import() only finds and require()s the file and you'll need to instantiate the class to use it. You can tell import() the type of class, the name and file path details.

ClassRegistry::init() loads the file, adds the instance to the object map and returns the instance. This is the better way to load something because it sets up 'Cake' things as would happen if you loaded the class through normal means. You can also set an alias for the class name which I've found useful.

Controller::loadModel() uses ClassRegistry::init() as well as adds the Model as a property of the controller. It also allows $persistModel for model caching on future requests. This only works in a Controller and, if that's your situation, I'd use this method before the others.