Showing posts with label cakephp beginners guide. Show all posts
Showing posts with label cakephp beginners guide. Show all posts

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.
  

Tuesday, 17 December 2013

Five Things that Every Beginner Should Know About CakePHP Framework


Image Courtesy: blog.koding.com

CakePHP framework is a quick build solution to PHP website development. As it follows MVC (Model View Controller) architecture, it has ability to divide development work in separate entities and hence supports the code re-usability feature. There are plenty of features that places CakePHP framework in a unique category, but in my research I find these five points that every beginner should know about CakePHP platform:

Easy to Create Admin Section
In CakePHP framework, you can easily create admin section that holds all rights in one entity by just uncomment “define(‘CAKE_ADMIN’,’admin’);” in config/core.php file.
That helps to access all processes prefixed with “admin_”, like “admin/controller/action”. You can also prevent unwanted user access through password protect your admin section.
 
Static Pages
It is easy to create and access static pages in CakePHP. Suppose you want to create a static page then just create a view inside the “views/page/folder”.  You can also access it through ‘/page’.
Also if you want to adjust the page title of static page using pages controller, add the given command “<? $this->pagetitle = ‘Page Title’; ?>” to view section.
 
Bake.php to Generate MVC
Bake is a command line php script that automatically produces a MVC on the basis of database design. It helps to save several times in repetitive tasks like making CRUD controller operation and views.
For using bake, you first crate a table in your database then convert’s directories to the “/cake/scripts/ folder” and run “php bake.php”.

Easy Email Sending Through PHP-Mailer
You can send mail either through JavaScript or PHP-mailer function. Which one is easy? The answer should be PHP-mailer because sending emails through PHP-mailer is very secure and it construct mail header by itself.
 
Many Options of Documentation
CakePHP has many documentation resources to learn and find solutions easily. Apart from manual, API and Wiki are two best sources of information. In these wiki tutorials and API, you can easily find all essential points to build a website in CakePHP.

Wednesday, 23 October 2013

Beginners Guide: Step by step Cakephp Installation Procedure on Xampp



Image courtesy: thebeak.edublogs.org
Now days, Cakephp web development is very popular for small as well as large projects, all over the world. Cakephp is popular because of its several advantages over other frameworks. Some of them are reusable code creation, MVC (Model View Controller) and strong debugging tools. Php developers found it best framework for web application development and content management.

Here i am going to teach you how to install and configure cakephp on your local xampp server. Follow the instructions below:

1. Go to cakephp.org and download the latest version of Cakephp (Latest version of cakephp is now 2.4.2 released today).

2. Copy the cakephp folder in xampp folder and unzip the file i.e. Local disk C--> xampp--> htdocs-->Cakephp folder.

3. Go to Cakephp folder -->app-->Config and rename the database.php.defult as database.php.

4. Make sure the host, login, password and database should be changed with proper values.
For example:

public $default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => '192.100.1.1',
            'login' => 'saurabhsharma',
            'password' => 'saurabh999',
            'database' => 'niche_site',
            'prefix' => '',
            'encoding' => 'utf8'
        );

5. Change the values of the Security.salt and Security.cipherSeed. You can find these two strings here: Cakephp folder - ->app-->Config-->core.php
/**
 * A random string used in security hashing methods.
 */
    Configure::write('Security.salt', 'DYhG93b0qyJfIyfs2guVoUubWwvniR2G0FgaC9mi');

/**
 * A random numeric string (digits only) used to encrypt/decrypt strings.
 */
    Configure::write('Security.cipherSeed', '76859309657458942496749683645');



6. Open the browser and type: http://localhost/Cakephpfolder/ or http://127.0.0.1/Cakephpfolder/ and You're Done..!

Note:  If you find still any error please check the “Cakephp folder -->tmp” is writable and “apache mod_rewrite” is enabled.