Freelance Jobs Freelance Jobs
Showing posts with label CakePHP. Show all posts
Showing posts with label CakePHP. Show all posts

Sunday, May 9, 2010

Installation of CakePHP


System Requirement

  • Http Server should be enabled
  • mod_rewrite should be enabled

Installation of cakephp

Follow the following step to install the cakephp:
  • Download the zip file from cakephp.org site
  • Create a new folder, which has same name as your site's name, in xampp/htdocs folder in your local drive
  • Extract the zip file in this folder


Database configuration in cakephp

Follow the following step to database configuration in cakephp
Create a new database in mysql or in database engine which you want to use.
Open the file xampp\htdocs\your_ site's_name\app\config\database.php
In this file there is two variable $default and $test are defined in class DATABASE_CONFIG
$default is used unless user does not specify another connection by $useDbConfig  property of a model.

Change the following code
'driver' => 'mysql',
  'persistent' => false,
  'host' => 'localhost',
  'login' => 'root',
  'password' => '',
  'database' => ' ',
  'prefix' => '',

driver: The name of database driver which is used to create database. You can use mysql, oracle, odbc etc. If you want to use datasource leave this field blank.

persistent : This is used to set database connection be persistent or not. If we set it true, the connection be persistent and if we set if false, the connection be non-persistent.

host: This is  host name of database's server.

login: This is login name of the database's server.

password : This is password of the database's server.

database: This is name of the database.

prefix: If the database tables has prefix, then set the prefix value here otherwise leave it blank

Tuesday, April 27, 2010

Introduction to CakePHP

CakePHP is open source web developing framework of PHP for creating web applications. It provides programmer a foundational structure to create a web applications.

CakePHP is based on the MVC architecture to create a web applications. It means that business logic(Model), user action(controller) and data presentation to user(View) are different from each other.

Controller: Controller contains the logic of user's application. The different functionality are contained by each controller. It used models to retrieve data from database or save data to database. It also set variable which is used to render data in view file.

Models: Models directly connected to database tables. It retrieve data from database and send it to controller, get data from controller and save it to database. Models behave like medium between controller and database tables. The models is interact with controller but not interact with view.

Views: view is used to present data to end user. It gets the variable from controller and display it to end user. View contains layout, element, for data presentation but no logic to retrieve data from database.

Since cakephp is open source so any body can download it from www.cakephp.org and install it and can work. There is no need of license fee to work in cakephp. You have to knowledge of PHP to start work in cakephp.


Folder Structure

When we download the cakephp from site, it is a zip file. After extracting it, the following files and folder you should see:

  1. app
  2. cake
  3. vendors
  4. .htaccess
  5. index.php
  6. README

All the applications file of the user are placed in app folder.
All the file of cakephp are placed in cake folder.
The third party files are placed in vendors folder.


App folder:

The app folder contains following files and folder:

config :
The config folder contains the following files and folder

database.php : It is used to database configuration.

routes.php : It is used to set up routes to user controllers and their actions. The routes is a mechanism which allow user to freely connect different urls to chosen controllers and their actions.

bootstrap.php: This file is automatically loaded by the app/webroot/index.php file after the core bootstrap.php. This file is used to include or require any files in user's application. This is application wide file that is used to load any function that is not used within a class define.

core.php : It is used to configure core behavior of cake.

controllers: It contains the controller of the user's application.

locale: It contains the files that is used for internationalization.

models: It contains models, behavior of the user's application

plugins: It contains plugin package of user's application.

tmp: It contains temporary file of user's application. This folder is used to store model descriptions,
log, caching, session information.

vendors: It contains the third party files to used in user's application. The file placed in this folder can be accessed in application using App::import('vendors','name');. The vendors folder is placed also at top level. The top level vendors folder contains third party file for user's all application but if user is creating multiple applications then he can placed third party files in this folder to use for specific application.

views: It contains view file, layout file, elements, error page of user's application.

webroot: This folder serve as document root for user's application. It contians css stylesheet, javascript file, image for user's application.

Monday, April 26, 2010

Validation in CakePHP

In a website, some time user need to insert data into database. User can insert inappropriate data to database, different from the database data type or requirement of the site. So, we need to use validate data before inserting it into database.
Suppose in user registration form, there is field like user name, password, confirm password, first name, last name, email id. User name require only alphanumeric character and not empty field, email should be valid email address and not empty field, password should be at least 8 character and not empty field, confirm password should match with the password field, first name and last name should be alpha character and not empty field.
If we will not validate data according to condition of certain field, then whatever user insert, it will store into database. This is not a good programming practice.

Validation is require to check user's data before storing in database. For validation, we check user's data according to condition specific to field.

As we know, cakephp is a framework of php which is a model-view-controller architecture. If you don't have basic knowledge of cakephp, first you have to learn about it.
In cakephp, we use validate variable to set the condition for validating user's data. We write this variable in our model. Suppose we want to validate user registration form, then there will be user model, users controller, registration view file in our cake php.


In user model

var $validate=arra('uname'=>array('notEmpty'=>array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
),
'alphaNumeric'=>array(
'rule' => 'alphaNumeric',
'required' => true,
'message'=>'only alpha and number are allowed',
'last' => true
),
),
'passwd'=>array('notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
)),
'email'=>array('notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
)),
'fname'=>array('notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
)),
'lname'=>array('notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
)),
);




In users controller, register function

function register() {

if(isset($this->data))
{
$this->User->set($this->data);

/*$this->User->validates() use to validate user's data according to validate variable in user model*/
if($this->User->validates())
{
$this->User->save($this->data);
$this->Session->setFlash('Data has been saved');
$this->autoRender=false;
$this->redirect('index');
}
else
{
$this->set('errorMessage','Please correct below error');

}
}
}


Register view

$form->create('User',array('action'=>'register'));
print ""; print $form->label('User Name'); print $form->text('uname',true).$form->error('uname'); print $form->label('Password'); print $form->password('passwd').$form->error('passwd'); print $form->label('Email'); print $form->text('email').$form->error('email'); print $form->label('First Name'); print $form->text('fname').$form->error('fname') print $form->label('Last Name'); print $form->text('lname').$form->error('lname'); print $form->end('Register'); ?>


$form->error('uname') is used to display error in the output. If we are using $form->input then we don't need to use this variable to show error. But if we are using $form->text, then we need to use $form->error to show the error in the output.
In $form->error('fieldname'), fieldname is your database field name as model is automatically connected to database table in cakephp.