Step 1

Enter your contact information.

Step 2

Enter your project information.

Step 3

Choose the PHP programmers you want to hire.

Compare Free PHP Quotes





Country



Kohana 3.1 Wiki Tutorial

*Attention PHP Programmers* - Add A Free Listing! Get more clients.

Win a new Compaq Presario Notebook!

In this tutorial you will learn how to create a simple wiki using the PHP framework Kohana version 3.1. Several years ago Siddharta Govindaraj created a screencast demonstrating how to create a wiki using Django, this is essentially the Kohana version of that. It covers using Kohana’s Routing, ORM and MVC systems. Full source code of the application is available on github.

The wiki application has 3 different pages:

Configuration

We will be using the ORM module to query the database for our wiki pages so we must load it in the application/bootstrap.php file (Line ~99).

Kohana::modules(array(

	'database'   => MODPATH.'database',   // Database access
	'orm'        => MODPATH.'orm',        // Object Relationship Mapping

	));

We also need to specify our database configuration settings. This is done in application/config/database.php.

<?php

return array
  (
  	'default' => array
  	(
  		'type'       => 'mysql',
  		'connection' => array(
  			'hostname'   => '127.0.0.1',
  			'username'   => 'root',
  			'password'   => 'root',
  			'persistent' => FALSE,
  			'database'   => 'query7kwiki',
  		),
  		'table_prefix' => ',
  		'charset'      => 'utf8',
  		'caching'      => FALSE,
  		'profiling'    => TRUE,
  	),
  );

  ?>

Model

Compared to most ORMs (Django, Doctrine, Propel), Kohana’s ORM is relatively light. You don’t need to specify the fields of your table as the Kohana ORM doesn’t attempt to generate the tables for you. It simply maps attribute names of the model object to field names in the database table. Be sure to check the Kohana Documentation for a detailed explanation of the ORM.

application/classes/model/page.php

class Model_Page extends ORM
{

	protected $_table_name = 'page';

	public function rules()
	{
		return array(

			'title' => array(
						array('not_empty')
							)

		);
	}

}

Execute the following SQL:

CREATE TABLE `page` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 255 ) NOT NULL ,
`content` TEXT NOT NULL
) ENGINE = MYISAM ;

Routing

I define all routes at the bottom of application/bootstrap.php, however if you are working on a large application it may beneficial in the long run to define them in a separate file and then include them into the bootstrap file.

Route::set() accepts 2 parameters. The first is the name of the route and the second is the URI pattern it should match. Because Route::set() returns an instance of Kohana_Route, we can chain the defaults method onto it. There we specify the controller and action that should be executed if the route is matched.

The <page> section of the URI pattern is captured from the URI and automatically passed to the controller method as an argument.

Route::set('wiki-edit', 'wiki/<page>/edit')
	->defaults(array(
		'controller'=> 'wiki',
		'action'	=> 'edit_page',
	));

Route::set('wiki-save', 'wiki/<page>/save')
	->defaults(array(
		'controller'=> 'wiki',
		'action'	=> 'save_page',
	));

Route::set('wiki-page', 'wiki/<page>')
	->defaults(array(
		'controller'=> 'wiki',
		'action'	=> 'view_page',
	));

Route::set('default', 'wiki')
	->defaults(array(
		'controller' => 'wiki',
		'action'     => 'view_page',
		'id'		 => 'index',
	));

Views

application/views/single.php

<html>
	<head>
		<title><?php echo $page; ?></title>
	</head>

	<body>

		<h1><?php echo $page; ?></h1>
		<p>
			<?php echo $content; ?>
		</p>
		<hr>
		<p>
			<a href="/query7kwiki/index.php/wiki/<?php echo $page; ?>/edit">Edit</a>
		</p>
	</body>
</html>

application/views/create.php

<html>
	<head>
		<title><?php echo $page; ?> - Create</title>
	</head>

	<body>

		<h1><?php echo $page; ?></h1>
		<p>This page does not exist. <a href="/query7kwiki/index.php/wiki/<?php echo $page; ?>/edit/">Create?</a></p>

	</body>
</html>

application/views/edit.php

<html>
	<head>
		<title><?php echo $page; ?> - Edit</title>
	</head>

	<body>

		<h1><?php echo $page; ?> - Edit</h1>
		<p>
			<form method="POST" action="/query7kwiki/index.php/wiki/<?php echo $page; ?>/save/"">

				<textarea rows="10" cols="60" name="content"><?php echo $content; ?></textarea>
				<br />
				<input type="submit" value="Edit Page" name="submit">
			</form>
		</p>
	</body>
</html>

Controller

The wiki only has one controller, Controller_Wiki, located in application/classes/controller/wiki.php. All of the methods accept one parameter, the name of the page which is automatically passed by the URL Router.

The action_view_page() method will display the wiki page if it exists or a ‘create’ page if it does not exist. $single->loaded() will return true if the ORM returned a result, so if the page exists we load the ‘single’ view and attach the appropriate variables to the view, if it does not exist then we load the ‘create’ view.

public function action_view_page($page)
{

	$single = ORM::factory('page')
			->where('title', '=', $page)
			->find();

	if($single->loaded())
	{
		$v = View::factory('single');
		$v->page = $page;
		$v->content = $single->content;
				}
	else
	{
		$v = View::factory('create');
		$v->page = $page;
	}

	$this->response->body($v);

}

action_edit_page() returns the ‘edit’ page with the correct content field.

public function action_edit_page($page)
{
	$single = ORM::factory('page')
			->where('title', '=', $page)
			->find();

	if($single->loaded())
	{
		$content = $single->content;
	}
	else
	{
		$content = ';
	}

	$v = View::factory('edit');
	$v->page = $page;
	$v->content = $content;

	$this->response->body($v);
}

action_save_page() handles saving the page after the user submits the edit form. It first checks to see if the page exists. If it does exist then we update the content attribute of the model with whatever was posted in the form. If it doesn’t exist then we make a new page by instantiating a new model, assigning the title and content attributes and saving it.

public function action_save_page($page)
{
	$single = ORM::factory('page')
			->where('title', '=', $page)
			->find();

	$content = $this->request->post('content');

	if($single->loaded())
	{
		$single->content = $content;
		$single->save();
	}
	else
	{
		$new_single = new Model_Page();
		$new_single->title = $page;
		$new_single->content = $content;
		$new_single->save();
	}

	$this->request->redirect('http://127.0.0.1/query7kwiki/index.php/wiki/' . $page);

}

Full source code of the application is available on Github.

Source http://query7.com/?p=1632
Mon, 09 May 2011 06:09:52 GMT
Tags: kohana, php, Programming, Tutorials, Web Development,

*Attention PHP Programmers* - Add A Free Listing! Get more clients.


kohana


Kohana 3.1 Wiki Tutorial Routes

In the original Kohana wiki application the URLs in the views were hardcoded. If we wanted to change the domain name or restructure a route we would need to go through the application and change

php


Using Proxies with cURL in PHP

If you are new to using PHP and cURL please refer to the PHP cURL tutorial. It covers why you should using cURL (as opposed to file_get_contents) and goes over how to make cURL requests with a custom class.

Developing a blog with CakePHP

If you want to develop a blog from sctrach without using any CMS like WordPress or Drupal, then you’ve come at the right place! In this series, we will understand how to develop a blog using
the CakePHP

A Look at PHP 5.3 Frameworks – Symfony2

Ever since PHP5.3 was released we have seen several new frameworks pop up in the PHP community. In this series of articles we will look at and evaluate some of the next generation PHP frameworks. Today we look at S

Programming


Cussing in Commits: Which Programming Language Inspires the Most Swearing?

As any programmer can tell you, programming will make you swear. But did you know that writing C++ will make you swear considerably more than PHP or Python? Developer Andrew Vos was looking for a weekend project when he decided to grab some one million co

Amazon S3 Storage Now Handles Entire Websites

Cheap, cloud-hosted web servers are a key component of a distributed web. But sometimes you don’t need a server, you just need a cheap way to host your static files, like images and videos. That’s the gap Amazon’s S3 service has long fil

Who Swears the Most? How Foursquare Used Hadoop to Find Out

We told you who swears the most in their code, but what about in the real world? Foursquare, the location check-in service, has used its rather large dataset to graph the “rudest” places in the English-speaking world — Manchester, U.K. t

Members:
Lahore
id4brands Profile
id4brands

I am Graphics Designer, I have Experience in this Field more than 10 years. i mostly work in print field. Products Catalog, e-catalog, brochures, flyers, brands names. logos. business cards.

Lahore, Punjab PK
Nashville
Robinson Publishing Profile
Robinson Publishing

Nashville, Tennessee US
Petaluma
Skytec Hosting Profile
Skytec Hosting

Skytec Web Hosting is California based internet services company that specializes in web hosting solutions for corporate companies, small business enterprises, webmasters and individuals.

Petaluma, California US
Athens
erjon Profile
erjon

tattoo shop

Athens, Attica GR
San Fernando Valley
FLATLINE labs Internet Solutions Profile
FLATLINE labs Internet Solutions

Professional website development and design, e-commerce solutions, specializing in the LAMP stack.

San Fernando Valley, California US
Design Leads


PHP Programmers Valid HTML 4.0 Transitional Valid CSS!

Back Up Your Gmail Account With Gmvault

The more you rely on cloud-based services, the more important good backups become. If you're using G



This Week in Web – App Engine, Titanium Studio, Useful PHP Snippets

App Engine 1.4.3 Released

Python API Changes
Prospective Search



MPEG LA Starts the Search for VP8 patents

MPEG LA, the one-stop shop for motion video patent licenses, yesterday announced a call for patents



A Quick Guide to Using HTML5 Forms

Web forms are everywhere—contact forms, comment forms, sign up forms—these days you̵



Getting Started With Twitter’s Embedded Tweets Feature

Somewhat lost amidst the news of Twitter’s revamped interface is a slightly more interesting t



Article Tags
PHP Programmers Articles
Browsers| Web Standards| CSS| firefox| HTML5| Web Basics| Programming| Web Services| Mobile| JavaScript| Web Development| Visual Design| Multimedia| chrome| Google| responsive design| This Week in Web| CSS 3| Opera| Web Apps|
Friends:
Live Help Chat Software
Web Design Quote
Web Design