Showing posts with label php. Show all posts
Showing posts with label php. 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!