Kohana 3.2.x: Master Template Controller [step 1]

Let's create a content module that will handle both static and dynamic content:

First enable the module:

modules/application/bootstrap.php

Kohana::modules(array(
.
.
'content' => MODPATH . 'content',   // Content module
.
.
));

 

Second, create the master template controller:

modules/content/classes/controller/base/template.php

template_name = Kohana::$config->load('content.template.name');
		$this->template = 'template/'.$this->template_name.'/index';
		parent::before();

		if ($this->auto_render) {
			$this->template->title		= '';
			$this->template->content	= '';

			$this->template->styles		= array();
			$this->template->scripts	= array();
		}

	}

	public function after() {
		if ($this->auto_render) {
			$styles = Kohana::$config->load('content.template.style');
			$scripts = Kohana::$config->load('content.template.script');

			$this->template->styles = array_merge( $this->template->styles, $styles );
			$this->template->scripts = array_merge( $this->template->scripts, $scripts );
		}
		parent::after();;
	}

}

 

Define routes to call the content module:

modules/content/init.php

<?php defined('SYSPATH') or die('No direct script access.');
 
/**
 * Routes for Content
 *
 * @package……Content
 * @category…..Routing
 * @author…….Mauricio Otta
 * @copyright….PortNumber53.com
 */
 
if (! Route::$cache) {
Route::set('homepage-content', '(<language–>"/)()',
array(
  'language' => '(pt-br)',
  'nothing' => '',
))
  ->defaults(array(
  'controller' => 'content',
  'action'     => 'homepage',
));

 Route::set('static-content', '(/).html',
  array(
   'language' => '(pt-br)',
   'path' => '[a-zA-Z0-9_/\.=\-]+',
  ))
  ->defaults(array(
   'controller' => 'content',
   'action'     => 'static',
  ));
}

 

Define the base content controller

response->body('Controller_Base_Content::index();
');
	}

	public function action_homepage() {
		$this->template->content = 'homepage';
	}

	public function action_static() {

		$path = $this->request->param('path');

		if ($filepath = Kohana::find_file('views', 'content/static/' . $path . '.html')) {
			$filepath = 'content/static/' . $path . '.html';
			//$this->template->content = 'static here'.$filepath;

			$content = new Model_Content();
			$latest_content = $content->get_last_posts(10);

			View::bind_global('latest_content', $latest_content);
			$this->template->content = View::factory($filepath, array(
				'latest_content' => $latest_content,
			))->render();

			switch ($path) {
				case 'privacy-policy': $title = 'Privacy Policy'; break;
				default:
					$title = 'unknown: ' . $path;
			}
			$this->template->facebook_og['og:title'] = $title;
			View::set_global('title', $title);
		} else {
			$this->action_dynamic();
		}
	}

	public function action_dynamic() {

		$path = '/'.$this->request->param('path', '');

		$content = new Model_Content();
		$latest_content = $content->get_last_posts(10);
		View::bind_global('latest_content', $latest_content);

		$post = $content->get_post_by_url($path);

		if ($post && $latest_content) {
			$this->template->content = View::factory('content/main', array(
					'latest_content' => $latest_content,
					'content' => $post,
				)
			);
		} else {
			$this->template->content = View::factory('content/main', array(
					'content' => array(
						'type' => Content::TYPE_UNKNOWN,
						'description' => 'Content not found',
					),
				)
			);
		}
	}
}

 

Define the content controller (~/modules/content/classes/controller/content.php):

<?php
class Controller_Content extends Controller_Base_Content { }
?>

 

Store some configuration values for the module (~/modules/content/config/content.php):

 array(
		'name' => 'default',
		'style' => array(
			'media/css/3-column-px.css' => 'screen',
			'media/css/screen.css' => 'screen, projection',
			'media/css/print.css' => 'print',
			'template/default/css/style.css' => 'screen',
		),
		'script' => array(
			'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js',
		),
	),
);

 

 

……………………………………………………………

 

LAMP: Installing on Slackware 13.37 64-bit

Ongoing post:

 

1. Download Apache ( http://httpd.apache.org )

tar -xvjf httpd-2.2.21.tar.bz2

cd httpd-2.2.21

I like Slackware, but I also like the Apache layout installed in Debian distributions, so:

./configure --enable-layout=Debian--enable-module=most --enable-mods-shared=most --enable-ssl=shared
make
make install


2. PostgreSQL

./configure –with-perl –with-openssl

gmake all

gmake install

 

 

PHP

./configure –with-pgsql –enable-soap –enable-zip –with-mysql=/usr/local/mysql/ –with-mcrypt –enable-mbstring –with-gd –enable-ftp –with-jpeg-dir –with-png-dir –with-bz2 –with-zlib –enable-safe-mode –with-apxs2=/usr/sbin/apxs

make

make install

 

(ve) Installing LAMP on Debian Squeeze – 64bit

aptitude update

aptitude safe-upgrade

aptitude install mysql-server mysql-client

 

 

 

NGINX

aptitude install libpcre3-dev build-essential libssl-dev sudo

cd /opt/
http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.0.8.tar.gz
tar -xvzf nginx-1.0.8.tar.gz
cd nginx-1.0.8
./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access_log --with-http_ssl_module
--with-http_geoip_module --pid-path=/var/run/nginx.pid

  nginx path prefix: "/opt/nginx"
  nginx binary file: "/opt/nginx/sbin/nginx"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access_log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

make
make install
#PECL
apt-get install make php5-dev php-pear
aptitude install libcurl4-openssl-dev
pecl install uploadprogress
pecl install pecl_http

echo -e "extension=uploadprogress.so" > /etc/php5/conf.d/uploadprogress.ini
echo -e "extension=http.so" > /etc/php5/conf.d/uploadprogress.ini
 ##FIX APACHE

<?php

// Let's fix the document root because of a bug in Apache's mod_vhost_alias not
// setting up the correct $_SERVER['DOCUMENT_ROOT'] env variable.
$__mod_vhost_alias_fix_doc_root = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'domains' . DIRECTORY_SEPARATOR . $_SERVER['HTTP_HOST'] . DIRECTORY_SEPARATOR . 'html';

if (is_dir($__mod_vhost_alias_fix_doc_root)) {
        $_SERVER['__MOD_VHOST_FIX_OLD_DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'];
        $_SERVER['DOCUMENT_ROOT'] = $__mod_vhost_alias_fix_doc_root;
}

?>

(vps): Noexec and /tmp troubleshooting

Chrooted /tmp directory

There is also a new method that ensures that no processes currently accessing /tmp are interrupted in any way. This also ensures that your /tmp that allows execution is never accessible to currently running processes. This limits your exposure to possible exploits.

First, create a chrooted environment that contains a tmp directory that allows file execution:

root@vps01:~# mkdir -p /root/chroot /root/tmp
root@vps01:~# mount --bind / /root/chroot
root@vps01:~# mount --bind /root/tmp /root/chroot/tmp
root@vps01:~#

Next chroot into the environment you created.

root@vps01:~# chroot /root/chroot
root@vps01:/#

At this point, you are in the chrooted environment and can run any commands you need to. When you are done, simply type the command ‘exit’.

root@vps01:/# exit
exit
root@vps01:~#

Now you are back to your normal environment.

 

Kohana 3.2 tutorial : 2 – Setting up and basic Redirect controller

1. Edit bootstrap.php under “application”:

——————————

date_default_timezone_set('America/Los_Angeles');
Kohana::init(array(
        'base_url'   => '/',
        'index_file' => '',
));

——————–

2. Change the default route to a redirect controller (so we can deal with redirections for misplaced content/old content):

Route::set('default', '(<request>)',
	array(
		'request' => '(.*)',
	))
	->defaults(array(
		'controller' => 'redirect',
		'action'     => 'index',
	));

3.Create the redirect controller

/modules/redirect/classes/controller/redirect.php

<?php
 defined('SYSPATH') or die('No direct script access.');
/**
 * Handles all the redirecions that may happen
 *
 * @package      Redirect
 * @category     SEO
 * @author       Mauricio Otta
 * @license      Confidential. Internal use only.
 */
class Controller_Redirect extends Controller {

	static public function action_index() {
		echo "Controller_Redirect::action_index();<br />";
		echo Request::current()->param('request');
	}

}

4. Test it.

You should see something like this:

“Controller_Redirect::action_index();”

test with different URLs.

Notice the controller does not handle the Query String part ( http://php.net/parse_url )

 

 

TO-DO: Add query string handling

 

 

Kohana 3.2 tutorial : 1 – Installation

1. Go to kohana’s website

 

http://kohanaframework.org/

 

2. Download latest stable

 

$ wget http://dev.kohanaframework.org/attachments/download/1670/kohana-3.2.0.zip
--11:08:44--  http://dev.kohanaframework.org/attachments/download/1670/kohana-3.2.0.zip
           => `kohana-3.2.0.zip'
Resolving dev.kohanaframework.org... 173.245.60.22, 173.245.61.87
Connecting to dev.kohanaframework.org[173.245.60.22]:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1,043,281 [application/zip]

100%[==============================================================================================================================================================>] 1,043,281      1.53M/s             

11:08:45 (1.53 MB/s) - `kohana-3.2.0.zip' saved [1043281/1043281]

3. Uncompress it and move stuff around

 

$ unzip kohana-3.2.0.zip

 

$ ls -la
total 981
drwxr-xr-x   5 portnumber53.com www-data               7 Dec  9 11:11 .
drwxr-x---  57 portnumber53.com www-data              70 Dec  9 10:23 ..
-rw-r--r--   1 portnumber53.com portnumber53.com      27 Dec  9 10:56 .htaccess
drwxr-x---   2 portnumber53.com www-data               2 Dec  9 10:23 cgi-bin
drwxr-x---   2 portnumber53.com www-data               2 Dec  9 10:57 html
drwxr-xr-x   5 portnumber53.com portnumber53.com      10 Jul 25 03:26 kohana-3.2-master-1
-rw-r--r--   1 portnumber53.com portnumber53.com 1043281 Dec  9 11:08 kohana-3.2.0.zip

$ rm kohana-3.2.0.zip

$ mv kohana-3.2-master-1/modules/ .

$ mv kohana-3.2-master-1/application/ modules/

$ mkdir -p vendor/modules

$ mv kohana-3.2-master-1/install.php html/

$ mv kohana-3.2-master-1/index.php html/

 

 

 

4. Combine the example.htaccess

 

$ nano kohana-3.2-master-1/example.htaccess

$ nano .htaccess

 

 

5. Try opening your site:

http://truvis.co/

Kohana install.php file will report what you need to fix…. hopefully just file paths:

 

6. Fix those paths (you millage may vary):

$ nano html/index.php

———————————————–

$application = ‘../modules/application’;

$modules = ‘../modules’;

$system = ‘../vendor/modules/system’;

————————

<?php

/**
 * The directory in which your application specific resources are located.
 * The application directory must contain the bootstrap.php file.
 *
 * @see  http://kohanaframework.org/guide/about.install#application
 */
$application = '../modules/application';

/**
 * The directory in which your modules are located.
 *
 * @see  http://kohanaframework.org/guide/about.install#modules
 */
$modules = '../modules';

/**
 * The directory in which the Kohana resources are located. The system
 * directory must contain the classes/kohana.php file.
 *
 * @see  http://kohanaframework.org/guide/about.install#system
 */
$system = '../vendor/modules/system';

/**
 * The default extension of resource files. If you change this, all resources
 * must be renamed to use the new extension.
 *
 * @see  http://kohanaframework.org/guide/about.install#ext
 */
define('EXT', '.php');

 

 

7. Test until you get Oks everywhere 🙂

 

AppSumo: $100 for life per year on Amazon

I can sure use it

http://appsumoforlife.com/~OLTM

Win $100 A Year For Life At Amazon.com
10 lucky winners will get an Amazon.com $100 Gift Card every year for the rest of their lives! Ends soon!