This is Part 3 of a series of posts where I document my learning process in studying Nooku. I’m a total Noob to Nooku so this might help other Nooku noobs to learn the framework.
You can read the Introduction of Learning Nooku Here.
Update: Download the component at this stage here - com_awesome-part3.zip
In Part 2, I learned 3 things.
com_default.In this post I’ll try to use some Nooku magic by accessing resources from the database.
Although I haven’t delved deeper into Nooku’s RESTful architecture, the first thing a noob will encounter is the significance of singular/plural resources. In Nooku, we access a resource by specifying it in the view variable in the request. If the view is plural, Nooku will try to return a collection of resources. If thew view is singular, Nooku will try to return a single object.
This becomes more apparent when we use Models in Nooku. In Part 2, I was trying to simply display a “dashboard”. It’s not a resource but it’s supposed to display a collection of resources. A dashboard doesn’t fit the mold of “singular” or “plural”. I simply want to display anything in the dashboard, and in this case a simple text.
But by default, Nooku will assume that I’m accessing a Database resource and it will automagically fetch data from a database table jos_awesome_dashboards. To override this default behavior, I had to create com_awesome/models/dashboards.php and then I created a class ComAwesomeModelDashboards which extends the base non-magic Model in Nooku, KModelAbstract. The magics happen in KModelDefault.
Now that I understand how plural/singular resources are handled in Nooku, I’ll try to put that knowledge into use. I will now make com_awesome list resources from the database.
I’ll make a generic resource named item. If I try to access the “item” resources at this stage using index.php?option=com_awesome&view=items, I’ll get an error Table ‘database.jos_awesome_items’ doesn’t exist of the following query : SHOW FULL COLUMNS FROM. jos_awesome_items. So I created what it’s looking for, a table named jos_awesome_items.
Now that the table has been created, Nooku is looking for com_awesome/views/items/tmpl/default.php. So I also created it.
I have to populate the table jos_awesome_items with dummy content. But first I added 2 columns in the table: awesome_item_id(according to Nooku’s table field naming convention) as primary key and name as varchar. Then I populated the table with anything that came to my mind. I added Duck, Airplane, and Juice.

At this point, I’m assuming that Nooku’s magic will work. I haven’t created a Model for items yet, nor have I created a View for it. I just created a Database table and populated it with random data. I am assuming that Nooku will display the data automagically if I create a template for it. Here’s the template that I created:
com_awesome/views/items/tmpl/default.php
<ul>
<?php foreach ($items as $item): ?>
<li><?= $item->name;?></li>
<?php endforeach ?>
</ul>
Trying to access it using index.php?option=com_awesome&view=items, bahm! The list is there, Magically!

Now I hear many people saying “amazingly awesomeful!”
As of this stage, my feelings are mixed. In one hand I’m delighted that the most basic and elementary functionalities are taken care of by Nooku. On the other hand, I dread the time when I have to do something unconventional, I’ll need to override the Magics first so I can proceed with my unique app freely.
I already have a bad feeling when I created an empty ComAwesomeModelDashboards whose only purpose is to disable magic so I can display a dashboard.
Magic Frameworks make simple things really simple, but complicated things really hard. I can only hope it’s not true in Nooku. I’ll learn as I go on.
I just have to accept that there are Tradeoffs… I can’t have everything. But I can’t help but wonder if I can easily disable all the Magic that Nooku does and simply start with the bare minimum.
So now I’m just dealing with 2 files to display a collection of resources from the database. The dispatcher file and the template file. Everything else is automagically handled by Nooku. I guess it’s the same when displaying a single resource from the database. So now I’ll access a singular index.php?option=com_awesome&view=item.
In Part 2, I learned that accessing a singular item will make Nooku assume that you’re editing an object. In this case, Nooku is now looking for /com_awesome/views/item/tmpl/form.php. I created this file, and put this code there:
<?php echo $item->name; ?>
I accessed it by specifying an id in the URL index.php?option=com_awesome&view=item&id=1.

Great! It worked as expected. It displayed the text Duck which is the name of an entry in the database with id = 1.
In this session, I learned that Nooku is a Database centric framework. Most of the Magic I encountered in Nooku seems to revolve around Database CRUD(Create Read Update Delete). At this stage, I was able to experience how easy the “Read” part is. By simply creating database entries and accessing those entries directly from the Template, I was able to display data. I had no need to create controller, model, and view classes.
However, I also had a bad feeling about it. As a programmer, my experience has always been that CRUD is not as simple as it sounds. When dealing with real world apps, I usually customize every bit of the app. Basic CRUD goes to the trash. What I want to know is how Nooku can help me create complicated, unconventional apps.
I haven’t decided yet what to tackle next. It seems that I don’t want to delve on CRUD anymore as it is so elementary I might be wasting my time. So my next entry could be any topic, and it should be a little more advanced.
© Copyright 2010