Category Archives: Kohana

Kohana #1: Setting up a project

There are quite a few methods of working with Kohana, I’m just going to go with the most convenient, less error prone and simplest to set up.

Install Apache, PHP5 (or greater), MySQL and phpmyadmin

You have the choice of installing each of those individually and configuring everything by hand, or you can choose one of the one-click-installers available across the web. One of them is XAMP (Mac, Linux), I’m on Windows so I’m just going to go with WAMP. Feel free to just install whatever works best for you.

phpmyadmin is a web application for managing your database. Its free. There are other solutions out there and of course you can always just use the command line. WAMP comes with phpmyadmin pre-installed.

Installing a IDE

Why use a IDE? Code completion, class diagrams for easy navigation, refactoring, easy debugging and so on. If you’re not convinced it’s fine, you’re not alone. You can use notepad or notepad2 or if you’re on a Mac Textmate, on Linux vim or some other editor. Kohana doesn’t care.

Out of the IDEs you can choose from there’s Eclipse, Aptana (Eclipse Based), Komodo, and a few paid ones. I’m going to use Netbeans for the tutorials just because its the most stable of the bunch and setting up debugging is the easiest.

Setting up Debugging

I’m going to configure for XDebug, just because Netbeans comes already configured for it too; so we only need to configure the server side.

If you’re using WAMP start it up and go to http://localhost/. There just click the phpinfo link on the left.

If you are not using WAMP, create a file and add:

<?php echo phpinfo() ?>

Then open it.Verify you have PHP5.3 or higher. If you don’t, you have likely installed some old packages or the application you’re using is out of date.

Now we’re on the same page, Ctrl+A and copy everything (no, don’t go into the source code!). With everything in your clipboard go to this page: http://www.xdebug.org/find-binary.php and paste it. You will now receive precise and newbie-proof instructions for configuring XDebug.

You should now have your server configured to use xdebug. Unfortunately we need to help netbeans a little. In your php.ini file (the same one you just opened for configuring xdebug) add the following after the code you inserted just now:

xdebug.remote_enable=on
xedbug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000

These instructions enable xdebug to communicate with netbeans, and vice versa.

If you’ve made it this far, congratulations! You now have a working IDE with Debugging, as well as a test server.

Other tools will be covered later at the appropriate time.

Setting up a Kohana3 project

Get the latest version of Kohana here: http://kohanaframework.org/download

You should have a zip archive. Use any tool you have and extract its contents. If you don’t have one get 7-zip.

You should now hove the following: 3 directories (application/ modules/ and system/), a example.htaccess file and a index.php as well as a install.php file. Its also worth mentioning in the application/ folder you’ll find a bootstrap.php file. Short explanation

  • system/ contains all of kohanas personal stuff, don’t touch it; you can cleanly overwrite absolutely everything there with out making upgrading to newer Kohana versions a nightmare
  • modules/ are basically plugins
  • application/ is where you’ll be working! don’t worry about the 7 folders they are empty, for now
  • index.php is the entry point for your application
  • bootstrap.php is basically Kohanas configuration file (timezone, locale, routes, etc.)
  • index.php and bootstrap.php are both part of Kohanas request flow

So what about install.php? It’s for installing, and we’re going to do it now…

Copy the kohana/ folder that resulted from extracting the archive to WAMPs www/ directory (tip: left click the wamp tray icon for tons of shortcuts). Don’t delete WAMPs index file there! Now if you go to http://localhost/ you’ll see under “Your Projects”  kohana/. To make this more interesting, lets rename the project folder to demo/, this way we can do some configuring. You’ll likely be creating multiple projects while testing so this will be useful.

With that done go to: http://localhost/demo/

You should see the kohana install page. The objective this time is to get all green. Kohana won’t complain if its not all green but this tutorial is eco-friendly so we need to make it all green!

If you’re using WAMP it should be almost all green except for the pesky Rewrite_Module. To fix that just LeftClick the tray icon Apache > Apache Modules > (there’s a lot, just scroll down) > rewrite_module.

In views I’m going to use short tags so while you’re at it if you want to copy/paste my code go ahead and enable that too. This time PHP > PHP Settings > short open tags. When using short tags always remember to use this style and only this style:

<?= $variable ?>
<? if (something): ?>

By doing so you can easily convert them back to the long version for servers where they are not supported. Also, completely avoid their use outside views, it just doesn’t make sense. I do not use short tags because they are easier to write, but because when dealing with mountains of html (and other code) they result in far easier to read code. They also work well with the alternative block syntax, which I also use.

Anyway, once you’re all green, simply rename the install.php file to install.php.done, and while we’re at it rename the example.htaccess to just .htaccess Change /kohana/ in there to /demo/ too. Windows won’t let you rename it? Just open it in notepad, then choose File > Save as…, and type in the name you want in quotes, like so: “.htaccess”

Now open application/bootstrap.php and configure the basics you can. There’s more to that file, just skip what you don’t understand. For our demo/ configuration we’ll have to just edit:

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

to

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

Once you’re done. Try opening http://localhost/demo/ again. You should see a hello, world page.

Almost done. Remember its only this long the first time.

Opening the project in netbeans

Before I conclude this tutorial I’m going to tell you both how to open it and how to do a simple breakpoint debug of the project.

Open netbeans, then go to File > New Project > PHP > PHP Application from Existing Source. Make sure in the following dialog to select PHP version 5.3 and simply browse to the demo folder. Select Next, and for the run configuration just set the Index File field to empty (as in delete whats there). Finish.

Now with the project open, navigate to application/classes/controller/welcome and place a breakpoint (ie. double click on line numbers) on the hello, world code.

Now go to Debug > Debug Project. And this will invoke xdebug. By default netbeans will stop each time at the first line in the php execution, which in the case of Kohanas request flow, will be the first code line in index.php. Click Continue (F5) in the  debug toolbar to move forward. Xdebug should now have stopped at the hello, world line.

Congratulations on successfully completing the tutorial!

Advertisements