All posts in PHP

View Partials In Kohana 3

To create view partials within a Kohana view just use this bit of code..

<?php include(Kohana::find_file('views', 'path/to/partial')); ?>

Kohana 3 Routes and Request Class

Kohana 3 Routes and Requests

Kohana sports a powerful and lean Request class. The most useful I’ve seen of any PHP framework, it essentially takes a regular expression and routes it to the appropriate controller, directory, action, or other value using keys, this allows for some robust URLs and applications. Lets take a look at how to mimic Basecamp style URL’s in Kohana minus the sub domain. If there is interest I’ll add that in a later article, just let me know in the comments, yeah?

Basecamp Project Style Route

Route::set('projects', 'projects/
(/(/(/)))', array(
    'project_id' => '\d+'
  ))->defaults(array(
    'controller' => 'log',
    'action' => 'index',
));

This route will take a URL like projects/1/, and load up the Controller_Log.php file and fire the action_index. In fact anything after projects/1/ will be fired like the default route. So this allows you to use, projects/1/todos/add and fire the action_add in the Controller_Todos.php file. Even better if you want to scope your todos to the project referenced in the URL, you can set the project in the controllers before() method:

public function before()
	{
		parent::before();

		$property = ORM::factory('project')
						->where('id','=', $this->request->param('property_id'))
						->where('user_id', '=', $this->user->id)
						->find();

		if($property->loaded())
		{
			$this->property = $property;
			$this->template->property = $property;
		}
		else
		{
                        // Couldn't find the project
			$this->request->redirect('/');
		}

	}

Notice I grabbing the parameter from the URL using the param() method in the Request class using the specified key in the route.


$this->request->param('project_id');

You could grab any parameter this way:


$this->request->param('id');

Using this method in your actions allows you to never pass parameters to your functions again, thus you can change the order of your routes and the site will function the exact same way.

Kohana Framework

Assets – A PHP 5 Asset Combining Class

Assets takes multiple JavaScript or CSS files and combines them into one.

So mootools.js, drag.js, slider.js, and tips.js can become script.php?load=mootools,drag,slider,tips.

Rather then four script tags, you have one. I recently published this Open Source PHP class to combine different text files into one in real time. The benefit here is JavaScript management and page loading speed. Making a bunch of HTTP requests to external JavaScript will slow down the page load, if you don’t believe me just open your website and profile it using the WebKit Inspector.

Documentation

There are two ways to really use Assets.

Automatic

Assets::factory()->get()->render();

This method implies that you are calling the page with a parameter of “load” in the URL. The file names need to be separated by commas. Example: script.php?load=mootools,drag,slider,tips

Manual

Assets:factory()->add('file.js')->add('file2.js')->render();

This methods adds each file individually passed in using PHP. It is worth noting that you can also pass and array of file names into the add() method.

Notes

If you prefer to use the new Assets that is completely OK. You don’t have to use the factory() method. Also, I am open to feedback; in fact that’s half the reason I release my code for free is for people to tear it up and tell me what to improve.

Download

Assets is on GitHub