Kohana: Buffering output on view and rendering it before in the source code

Sometimes you need to hack the order the source code is going to be rendered in a view:

– You want an extra piece of CSS that only aplies to that view, so you think it's not worth creating a single CSS file and including in your mashed file

– You need the same as above for javascript

– Your mind has not completely grasped the way to organize code in MVC  🙂

 

 

Here's a way to do it:

 

in your view you add something like this:

<?php View::start_buffer('head_end'); ?>
<style>
    ::-moz-selection {
        background: #b3d4fc;
        text-shadow: none;
    }

</style>
<?php  View::end_buffer('head_end'); ?>

 

Then, by taking advantage of the wonderful cascading file system that Kohana gives you, you create a View.php class in your application folder that will need to extend the render() function (I'm just adding the relevant changes for this to work; you can have your own extra sauce):

 

class View extends Kohana_View
{
    static public $buffers = array();

    public static function start_buffer($buffer_name)
    {
        self::$buffers[$buffer_name] = '';
        ob_start();
    }

    public static function end_buffer($buffer_name)
    {
        self::$buffers[$buffer_name] = ob_get_clean();
    }

    public function render($file = NULL)
    {
        $rendered = parent::render($file);

        if (! empty(self::$buffers['head_end']))
        {
            $rendered = str_replace('</head>', self::$buffers['head_end'] . '</head>', $rendered);
        }
        return $rendered;
    }

}

 

The buffer names are completely arbitrary, you can make them any way to want.

I leave up to you to improve the way each buffer will be mapped to different locations in the source code.

Drop me a line with your changes

Kohana: core, adding submodules, customization

After cloning the repository, let's add the Kohana core, a few modules and customize the installation:

1. 

git submodule add git://github.com/kohana/core.git modules/system

git submodule add git://github.com/kohana/database.git modules/database

git submodule add git://github.com/kohana/cache.git modules/cache

git submodule add git://github.com/kohana/image.git modules/image

git submodule add git://github.com/kohana/pagination.git modules/pagination

 

2. Initialize the modules

 

$ git submodule init

 

$ git commit -m 'Added core kohana and initial submodules [cache,database,image,pagination]'
[master 1d5462d] Added core kohana and initial submodules [cache,database,image,pagination]
 6 files changed, 20 insertions(+)
 create mode 100644 .gitmodules
 create mode 160000 modules/cache
 create mode 160000 modules/database
 create mode 160000 modules/image
 create mode 160000 modules/pagination
 create mode 160000 modules/system
 
 
3. Push these changes
$ git pull origin master
From mt:~/domains/git.portnumber53.com/html/menospior.com
 * branch            master     -> FETCH_HEAD
Already up-to-date.
 
$ git push origin master
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 663 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
To mt:~/domains/git.portnumber53.com/html/menospior.com.git
   8bce6b1..1d5462d  master -> master
 
 
We need some more files:
 
 

 

 

 

 

 

 

$ mkdir html
$ wget https://raw.github.com/kohana/kohana/3.2/master/index.php -O html/index.php
–2012-07-12 00:46:18–  https://raw.github.com/kohana/kohana/3.2/master/index.php
Resolving raw.github.com (raw.github.com)… 207.97.227.243
Connecting to raw.github.com (raw.github.com)|207.97.227.243|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 3292 (3.2K) [text/plain]
Saving to: `html/index.php'
 
100%[=========================================================================>] 3,292       –.-K/s   in 0s
 
2012-07-12 00:46:19 (76.6 MB/s) – `html/index.php' saved [3292/3292]
 
$ wget https://raw.github.com/kohana/kohana/3.2/master/example.htaccess -O html/.htaccess
–2012-07-12 00:46:47–  https://raw.github.com/kohana/kohana/3.2/master/example.htaccess
Resolving raw.github.com (raw.github.com)… 207.97.227.243
Connecting to raw.github.com (raw.github.com)|207.97.227.243|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 517 [text/plain]
Saving to: `html/.htaccess'
 
100%[=========================================================================>] 517         –.-K/s   in 0s
 
2012-07-12 00:46:48 (24.1 MB/s) – `html/.htaccess' saved [517/517]
 
$ mkdir -p modules/application
$ wget https://raw.github.com/kohana/kohana/3.2/master/application/bootstrap.php -O modules/application/bootstrap.php
–2012-07-12 00:47:32–  https://raw.github.com/kohana/kohana/3.2/master/application/bootstrap.php
Resolving raw.github.com (raw.github.com)… 207.97.227.243
Connecting to raw.github.com (raw.github.com)|207.97.227.243|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 3612 (3.5K) [text/plain]
Saving to: `modules/application/bootstrap.php'
 
100%[=========================================================================>] 3,612       –.-K/s   in 0s
 
2012-07-12 00:47:33 (104 MB/s) – `modules/application/bootstrap.php' saved [3612/3612]
 
 
 
If you try accessing your site you should see an error:
———-

 

 

 

 

Warning: require(/bootstrap.php): failed to open stream: No such file or directory in /home/httpd/domains/menospior.dev/html/index.php on line 102

 

Fatal error: require(): Failed opening required '/bootstrap.php' (include_path='.:/php/library/php-redis:/usr/share/pear') in/home/httpd/domains/menospior.dev/html/index.php on line 102

 

 

 

———————
 
 
Let's adjust the environment:
 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Chage a few settings:
html/index.php

$application = '../modules/application';

$modules = '../modules';

$system = '../modues/system';

 

Customize some kohana settings:

../modules/application/bootstrap.php

date_default_timezone_set('America/Los_Angeles');

 

Kohana::init(array(
'base_url'   => '/',
'index_file' => FALSE,
'cache_dir'  => DOCROOT.'../cache'
));
Kohana::$log->attach(new Log_File(DOCROOT.'../logs'));
 

 

 

 

 

 

 

 

 

Now create the logs folder

$ mkdir logs

 

 

At this point you should have a 404 error as there's no controller:

 

 

HTTP_Exception_404 [ 404 ]: The requested URL / was not found on this server.

SYSPATH/classes/kohana/request/client/internal.php [ 87 ]

82 
83 		try
84 		{
85 			if ( ! class_exists($prefix.$controller))
86 			{
87 				throw new HTTP_Exception_404('The requested URL :uri was not found on this server.',
88 													array(':uri' => $request->uri()));
89 			}
90 
91 			// Load the controller using reflection
92 			$class = new ReflectionClass($prefix.$controller);
  1. SYSPATH/classes/kohana/request/client.php [ 64 ] » Kohana_Request_Client_Internal->execute_request(arguments)

  2. SYSPATH/classes/kohana/request.php [ 1138 ] » Kohana_Request_Client->execute(arguments)

  3. DOCROOT/index.php [ 109 ] » Kohana_Request->execute()

Environment

 

 

 

This is optional:

 

$ wget https://raw.github.com/kohana/kohana/3.2/master/install.php -O html/install.php
–2012-07-12 01:43:59–  https://raw.github.com/kohana/kohana/3.2/master/install.php
Resolving raw.github.com (raw.github.com)… 207.97.227.243
Connecting to raw.github.com (raw.github.com)|207.97.227.243|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 8411 (8.2K) [text/plain]
Saving to: `html/install.php'
 
100%[=========================================================================>] 8,411       –.-K/s   in 0s
 
2012-07-12 01:44:00 (19.5 MB/s) – `html/install.php' saved [8411/8411]
 
the step above download the install.php file that can be used to test your environment. There are a few things wrong in the script, but it can tell you if you env has all the bells and whisles to use Kohana
 
 

 

 

Problems you may want to fix:

The iconv extension is not loaded.

Uncomment the line

extension=iconv.so

in your php.ini file and restart Apache

 

Kohana requires mcrypt for the Encrypt class.

Uncomment the line

extension=mcrypt.so

in your php.ini file and restart Apache

 

Kohana requires GD v2 for the Image class.

Uncomment the line

extension=gd.so

in your php.ini file and restart Apache

 

Kohana can use the http extension for the Request_Client_External class.

Install the pecl_http extension

 

# pacman -Sy php-pear autoconf
# pecl install pecl_http
/usr/lib/gcc/arm-unknown-linux-gnueabi/4.7.1/cc1: error while loading shared libraries: libppl.so.12: cannot open shared object file: No such file or directory
pecl/pecl_http can optionally use PHP extension "iconv"
downloading pecl_http-1.7.4.tgz …
Starting to download pecl_http-1.7.4.tgz (174,503 bytes)
……………………………….done: 174,503 bytes
71 source files, building
running: phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
 

/usr/lib/php/modules/http.so

You should add "extension=http.so" to php.ini

 

 

Fix install.php folder checks:

 

<tr>
<th>Cache Directory</th>
<?php if (is_dir(APPPATH) AND is_dir(DOCROOT.'../cache') AND is_writable(DOCROOT.'../cache')): ?>
<td class="pass"><?php echo DOCROOT.'../cache' ?></td>
<?php else: $failed = TRUE ?>
<td class="fail">The <code><?php echo DOCROOT.'../cache' ?></code> directory is not writable.</td>
<?php endif ?>
</tr>
<tr>
<th>Logs Directory</th>
<?php if (is_dir(APPPATH) AND is_dir(DOCROOT.'../logs') AND is_writable(DOCROOT.'../logs')): ?>
<td class="pass"><?php echo DOCROOT.'../logs' ?></td>
<?php else: $failed = TRUE ?>
<td class="fail">The <code><?php DOCROOT.'../logs' ?></code> directory is not writable.</td>
<?php endif ?>
</tr>
 
 

 

Kohana 3.2.x: How to install taking advantage of submodules

I have improved my Kohana 3.2.x installation steps.

This new way takes advantages of submodules with Git thus making your life easier when upgrading to a new version, assuming it's backwards compatible.

This guide will assume you have the following structure for your website:

 

root/html   <!– public html folder

root/modules   <!– kohana files

root/source   <!– environment dependable files

 

 

Ready? here we go:

 

1. Create an empty git repository following this:

 

 

2. Create a submodule with Kohana's core using:

$ git submodule add git://github.com/kohana/core.git modules/system

 

3. Add all the modules you'll need:

$ git submodule add git://github.com/kohana/database.git modules/database

$ git submodule add git://github.com/kohana/image.git modules/image

$ git submodule add git://github.com/kohana/pagination.git modules/pagination

 

4. Initialize the submodules

 

$ git submodule init

Submodule 'modules/database' (git://github.com/kohana/database.git) registered for path 'modules/database'
Submodule 'modules/image' (git://github.com/kohana/image.git) registered for path 'modules/image'
Submodule 'modules/pagination' (git://github.com/kohana/pagination.git) registered for path 'modules/pagination'
Submodule 'modules/system' (git://github.com/kohana/core.git) registered for path 'modules/system'

 

5. Commit these changes

 


$ git commit -m 'Added initial submodules'
 
 

 

6. Create the bare minimum folder structure:
 
$ mkdir -p modules/application/classes/{controller,model}
$ mkdir -p modules/application/{config,views}
$ mkdir -p modules/application/{cache,logs}
$ chmod 0777 modules/application/{cache,logs}
$ echo '[^.]*' > modules/application/logs/.gitignore
$ echo '[^.]*' > modules/application/cache/.gitignore
 
 

7. Now we need index.php and bootstrap.php files:

$ mkdir html

$ wget http://github.com/kohana/kohana/raw/3.0/master/index.php -O html/index.php

$ wget http://github.com/kohana/kohana/raw/3.0/master/application/bootstrap.php -O modules/application/bootstrap.php

 

 

8. Fix file paths on html/index.php

<?php

 

$application = '../modules/application';
$modules = '../modules';
$system = '../modules/system';

 

9. Change the default timezone

<?php

date_default_timezone_set('America/Los_Angeles');

 

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

10. Add a default .htaccess

wget  https://raw.github.com/kohana/kohana/3.2/master/example.htaccess -O html/.htaccess

 

 

 

 

Great way to integrate SwiftMailer and Kohana 3

Just found this post:

http://www.flynsarmy.com/2010/06/integrating-swift-mailer-into-kohana-3/

by Flynsarmy

showing the simplest way to integrate SwiftMailer and Kohana 3

Create the following files/folders:
/modules/swiftmailer
/modules/swiftmailer/init.php
/modules/swiftmailer/classes

Inside /modules/swiftmailer/classes/ drop the official latest build of Swift Mailer.

Enter the following into init.php.

<?php
require Kohana::find_file('classes', 'Swift-4.0.6/lib/swift_required');

There. Wasn’t that easy? Remember to enable the module by adding it to your bootstrap.php file!

My current methods while using Kohana 3.0.8 – part 1

I’ve finally decided to get started on documenting how I’ve been using this wonderful framework.

So if you’re patient enough to deal with my crazy schedule (meaning this series may take a while to be finish), fasten your seatbelt:

First of all:

  • Know your environment (your public html folder, a private [but still accessible] folder; permissions etc);
  • Know the difference between server side and client side programming;
  • Download Kohana (http://kohanaframework.org/) 🙂
  • Have your favorite source code editor and ftp client close to you

For the sake of whoever wants to get started with Kohana, I’ll go over the basics (not programming) and I’ll also cover everything that got me breaking my head, so I have a copy outside my brain.

[To-Do: find short video of Sean Connery’s character explaining to Harrison Ford’s character why he wrote everything down on a book]

Playing around with Kohana v3

New Kohana version is out, grab it from here:

http://dev.kohanaphp.com/projects/kohana3/files

And again, extract to a folder on your web server.

On good thing to do is extract both "Application" and "System" into a folder accessible to the user your webserver runs (usually, apache, nobody, httpd) and change the paths inside index.php

$application = '../application';
$modules = '../../../data/kohanav3/modules';
$system = '../../../data/kohanav3/system';

If you got the paths right on your environment, you should see a message saying your environment passes all requirements and you that you should remove the install.php file

kohanav3_environment_testsIt’s late here…. so this is all I’m gonna do for now.

Working with Kohana PHP Framework v2.3.4

Let’s start working with Kohana (I’ve actually been working with it for a while and thought would be nice to share my experiences)!

First thing you want to do is download it and extract it to a folder on your webserver.

You can read about it and download it at: http://www.kohanaphp.com/

Right after extracting Kohana to your webserver folder, you should see the following when accessing it:

Kohana 2.3.4 - environment tests

Kohana 2.3.4 - environment tests

If you don’t see the output above, check your server settings and logs to help you figure what’s wrong. Feel free to drop me a message, if you need more help after that.

After you remove install.php like required above, refresh the page and you should see this:

Kohana 2.3.4 - Welcome to Kohana

Kohana 2.3.4 - Welcome to Kohana

Please take some time to go over the documentation at http://docs.kohanaphp.com/ so you don’t feel totally lost. 🙂

As time allows me, I will be writing more stuff about Kohana. This is just a first post to get people started.

http://www.kohanaphp.com/You
  • IGlr