Jul 7, 2008

Zend Framework is fun

I have been working as Web developer since November 2007. My areas of expertise are PHP and MySQL. Initially I was working with structural programming, putting everything i.e HTML and PHP in the same php file with php extension. Although I really like OOP right from begining when i was doing my BS from post graduate college kohat, The reason was our computer teacher who was always giving preference to OOP 'coz according to him we can't represent real things using structural programmin and that was the reason why OOP came into existance, but i didn't use it much.
Although we used a bit of C++ OOP at our Bs level, however the real practice we did was at our Masters level when we were learning Java. Its was very intrested working in java after studying c++ for two years. The reason was c++ console programming which had bothered me a lot.
I really fall in love with java when we were learning GUI(Java swing). That was the time when i decided that my final project will be in java. however time didn't allow us to do our project in java. So we turn toward Coldfussion and developed a small web based system for our department. If someone had asked me why we choose coldfussion, i was having no answer at that time as we were unaware of other language such as php and asp at that time.
hmm the discussion is going a bit long, em not a bit I think long enough.
So i was talking about OOP. Now i think i should turn towards my topic.
All the above discussion is because Zend is in OOP.
Zend team has done excellent job. The way they have done our job so easy is amazing. Zend_Db and Zend_Auth has impressed my lot so for.
Its pretty simple to manage database and perform authentication.
look at the following example
If you want to authenticate a user and save his/her information in session it is as easy as that.
1. First your need a connection to database as
$dbconn=Zend_Db::factory($config->db->adapter,$config->db->config->toarray());
Note: I am not going to discuss how to make configruation object. so go to the zend documentation and read how to do it.
2. Now you need to create Zend_Auth_Adapter_DbTable() instance as
$authAdapter= new Zend_Auth_Adapter_DbTable($dbconn);
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password');
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);
3. As you have created the adapter, so its time to authenticate
$auth=Zend_Auth::getInstance();
$result=$auth->authenticate($authAdapter);
if($result->isValid)
{
$data=$authAdapter->getResultRowObject(null,'password');
// the about line will retrive the entire row
$auth->getStorage()->write($data);
// this line store the entire row in session(Zend_Auth_Storage_Sesssion).
}
else
{
echo 'unvlid user name or password';
}
thats it as simple as that.
Now if you want to retrive this data anywhere you will need the following
1. Create Zend_Auth_Storage_Session() object as
$sess=new Zend_Auth_Storage_Session();
$data=$sess->read();
// this will read the entire row
3. printing the result
print_r($data)
// or you can print individual fields as
echo $data->fieldname;

No comments: