Tuesday, January 26, 2010

What I know

Once I had the framework set up on XAMPP and I had done the basic installation steps (set up the database, configs, bootstrap, etc.), I wanted to structure my Application in a good OOP and HMVC way. I created an "Application" controller to extend the Template controller, with before and after functions. All other pages will extend this controller, and it is here where I will start to program, since I want to set up an application-wide authorization system. I wanted to use Auth, ORM and database sessions, not something too extraordinary really, but it resulted in several head-banging problems. Below I will comment the problem and how I solved it, but I recommend you take a look at the final code at my ProjectX repository.
  • Auth will be application-wide, so I must check if a user is logged in first, and then "dispatch" them to wherever they had to go (according to their auth status and request). This meant the use of the Request class of HMVC fame. I managed to do it by adding the code to the Application controller's before function, with a Request::factory() to handle when the user is not logged in, similar to how shadowhad does it in wingsc.
  • When the database module is loaded, you can use the database session class, but there are problems. Firstly, Auth uses "Session::instance()" to create/request the current session, but this by default only uses the native session class. Even if you create a database session before, it will create a new native session (this is supposed to be so that you can have several sessions open). What I did to remedy this was change the default session type to database (hardcoding it in Kohana_Session, substituting 'native'). I know this is not the way to go, but it was an easy fix until somebody (maybe me) fixes it in a better way (for example, through the session config file, or adding "session_type" to the Auth::instance() function). Also important: THE VERSION OF AUTH IN KOHANA 3.0.3 HAS A BUG THAT DOES NOT ALLOW THE USE OF DATABASE SESSIONS! It directly calls $_SESSION, instead of using the session class. Use the latest version from Github.

Resources

This post is going to be a permanent Work in Progress with links to all the resources I used, and a small comment about each of them.

  • Unofficial Kohana 3.0 Wiki: This is a great starting point, with good pages and links. Many basic concepts are taught here in enough detail to get from zero to something in no time. It is missing some information and links (for example the Auth section is very limited), but in general it gives a very good idea of everything and gets you started.
  • Kodocs: I think this link is a static copy of the internal documentation application of Kohana (guide). It is better to use the internal version because the most important part of the docs is the API browser, which auto-generates the documentation from code. Here you can see how your Controllers, Models, Modules, etc. sum up with those of the core system and work together. Kohana 3 is all about extending the base system, so you can follow a function all the way back and see what it happening.
  • Kohana Forums: This is a great place to look for answers. If you're stuck with something, search the forum and you'll be pleasantly surprised with the amount of information that will be handed to you. The problem is that most questions are quite specific, and probably not exactly what you were looking for. You have to read several discussions before you begin to get a grasp of where your problem may lie. Participating in the discussions is quite helpful, since you can ask your question directly and because it creates new interesting debates. I love this post.
  • shadowhand's wingsc: This is the open source code of a real website created by shadowhand, the lead developer of Kohana 3. I tend to think that what shadowhand does is is the default "best practice" for coding with KO3 (since he practically created it). On the other hand, he is a very "core" programmer, in that he doesn't use modules (for exaample, he has authentication implemented manually instead of using Auth). Either way, it's a good resource.

Introduction

I'm starting a new web application project and have decided to use version 3 of the open source Kohana framework. The project isn't entirely open source (yet), but I wanted a place where I could keep a diary of everything I learn about the framework while I work on the project. As I think most of it would be useful to someone starting with KO3 I've have decided to make this blog, where I will share knowledge and code.

It would be great is you left comments on the posts, especially corrections or better ways of doing things. I have also set up a Gituhub account where I will share all the open source parts of the project code. If someone is interested in participating, I will look into adding them to the projects.