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 every single URL.
Thankfully the Kohana Route class comes with a method url() which allows us to generate URLs to routes we have specified in the application. In the first tutorial we defined the following routes in application/bootstrap.php:
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',
));
Before we can generate routes we need to set some configuration options in application/bootstrap.php (line ~82). We need to specify the base_url and index_file attributes in the array passed to Kohana::init(). The application on my local install is located at 127.0.0.1/query7kwiki/index.php, so my Kohana::init() statement would read:
Kohana::init(array(
'base_url' => '/query7kwiki/',
'index_file' => 'index.php'
));
In application/views/edit.php the form action attribute was hardcoded to the value /query7kwiki/index.php/wiki/. That can be replaced by:
<?php echo Route::url('wiki-save', array('page' => $page)); ?>
In application/views/single.php there is a…
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:
- /wiki/somepage/save – handles the saving of the page
- /wiki/somepage/edit – displays a form that allows the user to edit the page
- /wiki/somepage – displays the page
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,
),
);
…
Requirements
Template Class
The use of the Settings class is self explanatory. Just replaces the instances of Settings with your own configuration system (or just strings). All Twig settings are documented here.
class Template
{
public $context = array();
private $template;
public function __construct($template)
{
$loader = new Twig_Loader_Filesystem(Settings::get('template_directories'));
$twig = new Twig_Environment($loader, array(
'cache' => Settings::get('template_cache_directory'),
'debug' => Settings::get('debug'),
)
);
$this->template = $twig->loadTemplate($template);
}
public function setContext(array $context = array())
{
$this->context = array_merge($context, $this->context);
}
public function render()
{
return $this->template->render($this->context);
}
public function __toString()
{
return $this->render();
}
public function __set($key, $val)
{
$this->context[$key] = $val;
}
public function __get($key)
{
return $this->context[$key];
}
}
Simple Example
hello.html
Hey there <strong>{{ name }}</strong> welcome to the website.
Code in controller
$t = new Template('hello.html');
$t->name = 'Frank';
echo $t->render();
Output:
Hey there <strong>Frank</strong> welcome to the website.
Example with Inheritance
base.html
<h3>Welcome to the website<h3>
{% block content %}{% endblock %}
hello.html
{% extends 'base.html' %}
{% block content %}
Hey there <strong>{{ name }}</strong> welcome to the website.
{% endblock %}
Code in controller
$t = new Template('hello.html');
$t->name = 'Joe';
echo $t->render();
Output:…
All frameworks discussed in this post are full stack web frameworks coded in PHP such as Code Igniter, Symfony, Kohana, Yii and CakePHP. Some (but definitely not all) of the points below may apply to micro frameworks such as FatFreeFramework, Limonade, Glue and Slim.
When should I use a framework?
Frameworks should be used when constructing web applications. Any application that involves a database, forms, sessions, cookies or a remote service (such as Twitter or Facebook) will benefit from being powered by a framework. There is no need to use a framework for a website that has only one or two pages, nor for command line utility scripts.
What features do frameworks provide?
- Database abstraction. Frameworks can abstract away SQL-vendor specific features. This allows you to change your SQL database without needing to rewrite any code. For example, switch from MySQL to Postgres or MSSQL. Some frameworks can also handle database relations automatically, which saves you having to write JOIN queries.
- Cache abstraction. Rather than using backend specific caching functions (such as apc_add and apc_fetch), you use a generic caching class which has support for multiple backends such as Memcache, APC and XCache.
- Form management. Symfony2 and CakePHP allow
…
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 Symfony2.
Overview
Symfony2 is a full stack web enterprise framework created by well known PHP guru Fabien Potencier. The framework itself is divided up into several different packages (Bundles) which are tied together with a Kernel object. Being a full stack framework Symfony2 offers almost everything you will need out of the box:
- Model, View, Controller separation
- Database management with Doctrine2
- Templating with either straight PHP templates or Twig
- Testing with a layer built upon PHPUnit
- Emails with SwiftMailer
- Custom form, validation, auth, event and caching systems.
One thing that distinguishes Symfony2 from other frameworks is that it relies heavily on Dependency Injection and as a result alot of configuration needs to take place. The standard of the Symfony2 is top notch, it is clearly set out, commented and tested. The full stack nature of Symfony2 is also a big strength. If you were to start a high upkeep project, you could be sure that in 5…
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.
This tutorial will cover how to use different types of proxies – SOCKS4, SOCKS5 and HTTP with cURL.
Basic cURL Request
We’ll start by creating a simple cURL script that requests the web page checkip.dyndns.org and displays the result on the screen. The website just displays our current IP address, this is useful when working with proxies. Below is our very basic cURL request.
$url = 'http://checkip.dyndns.org/';
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
echo '<pre>';
var_dump($result);
echo '</pre>';
curl_close($c);
As we can expect, the output will be your IP address.
202.183.56.21
Using Proxies with cURL
To use proxies with cURL we need to set 2 additional cURL options: CURLOPT_PROXYTYPE and CURLOPT_PROXY. CURLOPT_PROXYTYPE should be set to either CURLPROXY_SOCKS5 or CURLPROXY_SOCKS4, depending on the type of proxy you are using. CURLOPT_PROXY should be set to the IP address and port of the proxy you are using.
If you are using an HTTP proxy then you…
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 framework. So, why CakePHP? CakePHP is a robust PHP Framework having an extensive community and excellent documentation. It’s based on the MVC pattern, which makes it really simple to use.
Moreover, you can add numerous functionalities to your web application or blog using this framework.
We would be covering the following topics in this series
- Installing and Configuring CakePHP
- Understanding MVC
- Creating a simple blog
- Adding advanced features to your blog – Understanding Models, Views, Controllers in Depth
- Understanding Helpers
- Ajaxifying comments, adding content summary and Gravatar to your blog
- Securing your blog
In this article, we’ll just take a look at installing and configuring CakePHP.
Download the latest version of CakePHP framework from CakePHP.org. Make sure to download the stable release and not the release candidate.
Unpack the contents of the Cake archive inside the “www” folder of wamp or mamp directory.You now have a folder in your document…
PHP5 CMS Framework Development is a 322 page book that covers all aspects of creating a full featured content management system (CMS) in PHP5. The author Martin Brampton has a history of developing extensions for the Mambo and Joomla CMS projects. He became lead developer of the CMS Mambo before starting his own CMS, Aliro. The book is split up into 14 chapters. The first is an introduction to CMS and PHP5 concepts and the remaining 13 each look at a key feature of the CMS.

The book starts by defining what a content management system is and what features it needs to have. It then gives some background into the Model View Controller and Object Relational concepts as well as the Factory, Singleton, Observer and Command design patterns. If you haven’t used a CMS before and don’t know PHP5 OOP, this chapter prepares you excellently for the rest of the book.
The remaining 13 chapters each cover a feature of the framework. This includes:
- Database abstraction
- Access control, user authentication
- Framework extensions/plugins
- Caching
- Internationalization
- Error handling
- Menus
Brampton has done a great job at explaining each feature before he shows any code. This ensures…
In this tutorial we will cover how to build a module from scratch in Kohana 3. If you aren’t familiar with the Kohana framework then I recommend you read the beginners introduction to Kohana.
The Plan
We will be building a module that replaces Kohana’s own view layer (Kohana_View). It will use the PHP template library Twig. We want our own view layer to be API compatible with Kohana’s view layer. This will ensure that other modules work out of the box and that the only code the developer will need to alter in their application are the templates (so that they are Twig compatible). No code in any controller will need to be modified.
Because Kohana follows HMVC (see beginners introduction to Kohana) we will be calling our class View. This means that all calls in the application to the class of View will be to the View class in our module, not Kohana’s own View class.
Directory Structure

Enabling The Module
The name of any Kohana module is the same name as the directory inside the modules directory. So in this case it is twig. To activate any module, we must add it…
This is a brief introduction into the Kohana framework. The next two tutorials will cover how to build a blog in Kohana.
Installation
Installing Kohana is very straight forward, just download the latest stable version of Kohana and unzip it. No need to mess around with yaml or xml files like other frameworks. Navigating to the directory on your web server should give you a page similar to…

Filesystem
The filesystem plays a big part in the operation of the Kohana framework. Kohana follows the HMVC pattern (hierarchical model view controller). This means that the same filesystem structure exists at multiple locations in the framework. The following directories exist in the application, system and in all module directories:
- classes
- config
- i18n
- messages
- tests
When calling a class in Kohana, the autoloader checks if the class exists first in application/classes/ , then in each of the installed modules, classes subdirectory and finally in system/classes/. If the file cannot be found then an exception is thrown. Kohana follows the Zend_Class_Naming style. For example, if you instantiated a class named Hello_World, Kohana would look at each of the following locations in this order. If it finds that…