Pages

Sunday, January 27, 2013

Cakephp Auth Component 2.0

Cakephp Auth Component 2.0

Define Auth component to controller as follows

public  $comonents=array(
'Auth'=>array(
                           'loginRedirect' =>array('controller'=>'users','action'=>'index'),
                           'logoutRedirect'=>array('controller'=>'users','action'=>'index'),
                            'authError'=>'Access Denied',
                            'authorize'=>array('Controller'),
                            'loginError'=>'Invalid credentials, please try again.',
                            'allow'=>('index','view');
                       ) ,
'Session'
);

//check for authorization
public function isAuthorized($user)
{
          return true;
}

Now in users controller define the login action

public function login()
{
       if($this->Auth->login())
       {
           $this->redirect($this->Auth->redirect());
       }
     else
    {
       $this->Session->setFlash('Authentication Failed')
   }
}

public function logout()
{
     $this->redirect($this->Auth->logout());
}

create form in login view to provide interface to user for username and password and AuthComponent will automatically validate login.

Make sure fields in the database match cake's cretriea i.e "usename" for User Name and "password" for Password and "Users" table.

register new user 
create a new add method in a controller create a view for the same and then in user model just before save hash the password as follows

$this->data['user']['password']=AuthComponent::password($this->data['User']['Password']);

To check for login status
$this->Auth->loggedIn();
To get current info of loggedin user
 $this->Auth->User;
To Display auth error message
$this->Session->flash('auth');

Scaffolding in Cakephp

Application scaffolding is a technique that allows a developer to define and create a basic application that can create, retrieve, update and delete objects.

To add scaffolding to your application, in the controller, add the $scaffold variable.

class CategoriesController extends AppController {
    var $scaffold;
}

Scaffolding a way to automatically look at what a table has and its very basic front end to check its CRUD Operations

Scaffolding automatically reads relations defined in a model

Foe example if your table has a field of tinyint attribute with length 1 then in that case cakephp will treat the same as a checkbox.

When we use a "hasMany" relation in a Model the form will show a dropdown

When we use a "has and belongs to Many" relation in a Model the form will show a multiselect list box

When we use a "belongsTo" relation in a Modelthe form will show a dropdown

The Scaffolding Structure(Form in view) shows the relation with an "Alias Name" given for the relation defined in a model.

Foe example if we have define a Table "Persons" and another table as "Horses" then we define the relation in a "Horses" model as

var $belongsTo=array('Owner'=>array('classname'=>'Person','foriegnKey'=>'owner_id'))

then Scaffolding for the  model will show the field name for owner_id as Owner.

also for a case like

var $hasAndBelongsToMany=array('Riders'=>array('className'=>'Person'));

and in Persons model we define

var $hasAndBelongsToMany=array('Horse');

we will find a multiselect list box with name "Riders" with automatically picked up names from Persons table.


MYISAM and INNODB difference

Differences between INNODB abd MYISAM Database
  • InnoDB uses row locking, MyISAM uses table locking.
  • InnoDB is slower when it comes to SELECT
  • InnoDB enforces referential integrit
  • InnoDB allows transactions
  • MyISAM allows full text search Indexes where as InnoDB does not allows that 
  • You will find better crash recovery in InnoDB. 

MyISAM Innodb
Not *ACID compliant and non-transactional *ACID compliant and hence fully transactional with ROLLBACK and COMMIT and support for Foreign Keys
MySQL 5.0 Default Engine Rackspace Cloud Default Engine
Offers Compression Offers Compression
Requires full repair/rebuild of indexes/tables Auto recovery from crash via replay of logs
Changed Db pages written to disk instantly Dirty pages converted from random to sequential before commit and flush to disk
No ordering in storage of data Row data stored in pages in PK order
Table level locking Row level locking

Friday, January 25, 2013

First file that gets loaded on using cakephp?Is it changable?

Bootstrap.php , yes it can be changed , either through index.php , or through .htaccess

Why cakephp then any other framework?

Why cakephp then any other framework?

Well, This might not be the perfect answer but I state it as follows

*Has been for years in the market with strong support in the form of communities and online documentation
*It supports PHP 4 and 5 , sometimes it becomes mandatory to support PHP 4 because of client's limitation in support PHP 5, there cakephp helps.

To be frank I had worked on Yii and Cakephp and the only difference I found was this however my further analysis stated that CI and Kohana also support PHP4.

Though every framework has its critics for example In cakephp if it supports PHP 4 OOPS the "privates can be accessed easily".

But since the question was positive and so was the answer.


What is Cakephp

What is Cakephp?
Cakephp is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications. 
It uses commonly known design patterns like MVC,ORM within the convention over configuration paradigm.
It also reduces development costs and helps developers write less code.

MVC=>MODEL VIEW CONTROLLER
ORM =>OBJECT RELATION MAPPING

ORM stands for a technique in which we turn every RELATION(Table) into an OBJECT and its ATTRIBUTES as data members of the class.

*If you are familiar with Entity framework in .Net consider the Db Context class which represents a database
*In regular MVC Model classes are created to represent Tables of the concerned database.