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

Parsing XML file using CakePHP

. Wednesday, August 6, 2008
6 comments

After spending one whole evening, I found out CakePHP has its own XML class for handling xml files. In the meantime, i tried SimplePie (for RSS only, not XML), SimpleXML, and XMLize. But still they werent of any great help compared to cakephp's core XML class.

How to parse it then?
First of all, you need to import the XML class in your controller class using App::import(). Here is the controller class parsing a particular xml file and printing out (like print_r) the returned array.

Controller class:

<?

class ParseController extends AppController {
var
$name = "Parse";
var
$uses = array('MyModel');

function
xml() {
// import XML class
App::import('Xml');

// your XML file's location
$file = "my_xml_file_location.xml";

// now parse it
$parsed_xml =& new XML($file);
$parsed_xml = Set::reverse($parsed_xml);

// see the returned array
debug($parsed_xml);
}
}

?>

Search Engine Friendly URLs with CakePHP

. Saturday, July 12, 2008
1 comments

This helper will let you create search engine friendly urls without having you to change the controller logic.

Lets assume we have an 'articles' controller (/app/controllers/articles_controller.php) in our website. And the database table looks like this:
id, category_id, title, content, updated, created

The current url structure:
yoursite.com/articles/category/[category-id]/
yoursite.com/articles/view/[article-id]/

So, it means we dont have the 'title' of our articles included in the url. Only ID is present. This helper will let you add the title as well in the url.

Helper class

<?
class SefHelper extends Helper {

var $helpers = array('Html');

function clean($string) {
return r(" ", "-", strtolower($string));
}

function article($array) {
$link = "/articles/view/";
$link .= $array['Article']['id'] . "/";
$link .= $this->clean($array['Article']['content_title']) . "/";

return $link;
}

function category($array) {
$link = "/articles/category/";
$link .= $array['Category']['id'] . "/";
$link .= $this->clean($array['Category']['title']) . "/";

return $link;
}
?>


View
<? echo $html->link("Article Title here", $sef->article($articles_result)); ?>

The return of this helper function will be: /articles/view/[id]/[title-of-the-article]

Scaffolding in CakePHP

. Monday, June 9, 2008
9 comments

What is scaffolding?
Its something that you will find very useful in production apps. When you begin, you may want to throw up stuff real quick in order to get started. Ultimately, its a built in system for adding, editing, viewing and deleting your database records quickly.

Database table
Suppose we want to work with a table named users, and here is its SQL structure

CREATE TABLE `users` (
`id` bigint(20) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`email` varchar(96) NOT NULL,
`password` varchar(50) NOT NULL,
`updated` int(11) NOT NULL,
`created` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
)

Model
Now lets create a model file first. It should be placed here: /app/models/user.php
<?
class User extends AppModel
{
var $name = "User";
var $uses = "users" // table name
}
?>


Controller
Create a controller file here: /app/controllers/users_controller.php
<?
class UsersController extends AppController
{
var $name = 'Users';
var $scaffold;
}
?>

View
Not required since the views for scaffolding are generated by CakePHP automatically.

Visit http://yoursite.com/users/ and start scaffolding.
Note: Always visit yoursite.com/controllerName/ to see the visual output of your controller.

I will show you a user registration and logging system in CakePHP soon.

Installing CakePHP

.
0 comments

Download
Get the latest version of CakePHP (current version is 1.2)

Requirements
A HTTP server (better if Apache) with session and mod_rewrite enabled.
PHP 4.3.2 or higher
A MySQL database (PostgreSQL is supported too)

If you want to install it in your PC (Windows), then read this article: http://frinity.blogspot.com/2008/05/installing-apache-php-mysql-in-windows.html

Upload the files
Unpack the downloaded file, and upload its content to your server. Make sure that it looks like this from your root.

/wwwroot
/cake
/app
/cake
/vendors
.htaccess
index.php

Configure your database
Rename app/config/database.php.default to database.php. Open it and edit the details.

var $default = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '');


CakePHP installed!
Now we can move to another article for creating your first controller!

Understanding the MVC framework (PHP)

.
0 comments

I have already mentioned about MVC framework and CakePHP in one of my articles, therefore I am going to discuss more about it today. Please note, CakePHP is one of the MVC frameworks written in PHP. Don't get confused that MVC framework means CakePHP.

MVC
MVC stands for Model View Controller. It is ultimately a software design pattern where you can separate your code and make it reusable, thus making you write less code.

Model
For each and every database table, we have a separate model - this is what represents a particular database table. And according to the naming convention of CakePHP, its name is always the singular of its table's name. For example, if the name of the database table is 'users', the model name will be 'User' (first character of the name is always in uppercase).

Controller
A controller is used for managing the logic of a certain section of your application. Generally, it is used for managing a single model. For example, you have a model named 'User', and you will require a controller for managing the logic of user registrations, logins, log outs, account settings, etc. According to the naming convention of CakePHP, controller names are always plural. In this case, the controller name would be 'Users'.

View
A view is a page template, usually named after its controller's action (a controller is a php class with many functions known as its actions). As you can guess, this is responsible for the visual output of your application.

I will show you how to install CakePHP (including the php files) in my next article.

Why choose CakePHP over other frameworks?

. Thursday, June 5, 2008
0 comments

Those who have the slightest experience of Ruby on Rails would understand the importance of MVC (Model View Controller) framework for developing web applications. RoR is awesome, there is no doubt at all. But as a web developer, I have to build websites for others and this means I have to use their web host. Unfortunately, there are a very few web hosts supporting Ruby (compared to PHP).

When it comes to MVC framework in PHP, you would immediately think of CakePHP, CodeIgniter, Symfony or Zend Framework. Now the question is which one serves the purpose best for you.

Server Compatibility
If you want your application to run in most servers, then you cant rely on Symfony much since it requires PHP5 while CakePHP and CodeIgniter both run in PHP4.4.x to PHP5. If you want search engine friendly urls, then you will require mod_rewrite module for apache (not a must though - you can still run it without mod_rewrite).

Documentation
To be fair, CodeIgniter is more organized in terms of documentation, and CakePHP is still updating its Cookbook (for version 1.2 beta). They have Screencasts that beginners will find useful. Most importantly, their community is very strong, and you will get quick response if you get into any problem.

Support
CakePHP has a pretty active community as well. They have the Bakery where users can participate and submit articles, code snippets, etc. Moreover, there is a Google Group and IRC channel where you can ask for help anytime.

I am a big fan of CakePHP, and will stick to it. According to Google Trends, there are more people searching for CakePHP than any other popular php frameworks. I will always support going for that open source project that has a huge community (or has the potential).