The Blog of...

This is my blog, hope you get something out of it. There's post on most things that interest me, but I'm sure you'll see the bias towards, code and japanese and occasionally my Wii.

As well as that, you can find more about me, a great chilli chocolate chip cookie recipe and some info on how I build this site.

Latest Posts

For posts from all time please look in the vault.

MVC framwork - from scratch!

So, I started a new job. And the first thing I get to work with is a large PHP application for managing Wireless Hotspots. The application presents a portal page to people trying to connect and handles payment and creation of the Radius info to allow them to use the service. The PHP element is mainly a large and complicated database frontend, with a few special functions to interface with some other systems. Anyway, the thing was originally written about 5 years ago in PHP 4.2.2 and the code makes me shudder.

So, I gave some thought to how I would do it if I was writing the same thing from scratch. I immediately thought "Cake" but then realised that for a commecial application I would want to fully understand all the under-the-hood business and as good as CakePHP is, I don't have that full understanding. So instead I thought about writing a framework from scratch - albeit based on many CakePHP principles.

Requirements

The first thing to think about is what you want the framework to achieve on it's own before we start to think about building the app on top of it. This project has some features which must be available in the framework.

  • Multiple points of entry:

    The application is accessed from 3 different places, and they will share much code.

    • The user interface is what a person trying to connect to the wireless network would see.
    • The customer interface is a Customer self-care portal for a hotspot provider to manage their hotspots.
    • The admin interface allows management of the entire system and reporting.
  • Multiple Databases: specifically, their are 2 databases in use, the "main" one and the Radius AAA database.

  • Authentication and Accesss control: for the 2 restricted interfaces, this should be ingrained into the framework at a low level, but extendible to accept different authentication mechanisms.

  • Licensing: this product may be sold under license so the ability to restrict capabilities is required.

Design

The next think to think about is how the framework will be put together. I leaned heavily on the cake approach in designing the basic structure of the framework. I tried to imagine the flow of a request and what I would need to build to make it work. So I got the following:

  1. Index.php: accepts the request set's some paths, loads a common "boot.php" file.
  2. Boot.php: loads the generic functions (including a "Loader" class for autoloading other classes), loads some config options from "ini" files, creates a Dispatcher instance and starts the ball rolling.
  3. Configure.php: handles the config options loaded, a "Globals" replacement.
  4. Dispatcher.php: co-ordinates the request. Processes POST data, gets info from Router class, and instantiates our Controller, and creates and renders the View.
  5. Router.php: Router class does forwards and reverse routing, extension parsing and parameter handling.
  6. Controller.php: has methods to set options for the view.
  7. View.php: takes the request options, controller settings and renders the appropriate template / layout.

Then there's all the other useful side classes Session, Cookie, HTML, Money and I haven't even got onto to Models yet.

Next time

I might have made a diagram to explain how this works, or myabe will be ranting about the Model parts, and multiple database access. To be honest to point of this article was not so much about the actual code, but more the structure and how if you break it down, a framework with much of the principles of Cake can be put together relatively easily. Of course I am only half way through, but progress has been quick.

In conclusion, I found it extremely interesting to see how the varous part of a framework work together and it is very satisfying to see it all fall into place. I now have the basis of a web-application framework that I understand inside out because I wrote every line of code. That is the benefit. When I use this now I will be supremely confident that I understand every nuance and feature.

Comments:

Leave a comment

No more Japanese

Not an apocalyptic incident, just that my Japanese course has finished for the summer, which is a shame because I was enjoying it and now I have to keep that momentum up myself before they restart in October.

So, I thought I'd write something about it to encourage myself. Also if you live anywhere around Exeter (Devon, UK) then you should sign up at Exeter College, because we could use the numbers and Noriko's classes are excellent.

Learning Japanese

When we first started we were told about "how easy it is to learn Japanese" and some of the arguments are quite convincing. I studied Latin at school, which is about as complex as I thought a language could get. A lot of the things that surprised me about learning Japanese is just how different the structure of the language is to anything I had seen before (i.e. Romance and Germanic languages).

Why Japanese is easy to learn:

  1. There's no articles (i.e. a book, or the book, just "book")
  2. There's no gender or plural. Objects just are, they have no male/female/neuter preferences and the word for one is the word for many.
  3. There's only 2 tenses, past or present/future.
  4. Questions are formed form statements by simply adding the word "ka" at the end. No re-arranging of words / sentence structure.

Which was great and we're were all thinking how good this was going to be. Then we actually started learning...

Why Japanese is difficult to learn:

  1. They have 4 differents character sets:
    • Romaji: Japanese transliterated into Roman characters
    • Hiragana: 46 symbols indicating sounds, some of which can be combined, and some of which can be accented, making about 100 sounds. These characters are used to write "Japanese origin" words.
    • Katakana: A different set of 46 symbols, as above, but used to write words of "non-Japanese origin".
    • Kanji: the pictograms adapted from Chinese that represent ideas, there are about 2000 standard Kanji, but many, many more exist.
  2. The structure of the language is nothing like any language I have learnt. For example they have no verb "to have", so instead of saying: "I have this thing", they say "This thing exists" which doesn't really sound right to me and I can't get my head around it.
  3. Verbs are strange. They have 4 basic forms, which I don't really understand at all.
    • the "dictionary" form is only used in a dictionary as far as I can tell.
    • the "-masu" form is a polite form
    • the "-tai" form indicates a want to do the action of the verb
    • the "-te" form indicates some other meaning, and is also used when using more than one verb in a sentence to "and" them together.

And there's probably some more I can't remember right now. Basically it means that learning is pretty hard for me. I like structure and rules. I leanr languages by understanding the grammar and I don't understand Japanese grammar.

This is by no means a critisism of our Sensei, she's been excellent, but because of the complexity and just how different it is, Japanese is not taught by grammar, but in a more fuzzy way, particularly to start with.

In turn this then means that I have been learning it for an academic year, yet can say very little, and cannot form anything but the most basic sentence, and only with predefined verbs. It's very frustatrating.

On the other hand, it is an absolute joy to be able to read and write in a brand new alphabet and learning hiragana and katakana has been very rewarding.

To aid my learning I fell back on my other skill, code, to create a web based Kana Test, to practice learning the Kana. Unfortunately, Julie, on our course completely obliterated any score I was able to manage and now reigns at the top of the scoreboard, no-one able to budge her off...

You can try it out if you want: Hiragana/Katakana Recognition Tests

Still, I am looking forward to continuing next year, and if I'm up to it, I might enter myself for a GCSE!

Comments:

Leave a comment

DarkAuth update, and test app.

I found another small glitch in DarkAuth, which stemmed from the major bug after CakePHP 1.2 RC1 was released.

The Controller::render() method has changed, which was integral to DarkAuth's functionality. The change stopped anything from being displayed if Auth was not present or sufficient. To fix it I had to force an exit() into the code.

This exit() then meant that the Component was not setting the $_DarkAuth variable at all, if the "login" or "deny" view where being displayed.

The updated code fixes this, and has been updated on the DarkAuth page.

In addition I introduced a nice utility method, DarkAuthComponent::getUserId() which simply the returns the id of the currently auth'd user. Something I needed often.

Also I decided it would be good to have a "test application", a simple setup of a working DarkAuth + Cake combo. So I have built one, and you can use it to see how DarkAuth works, and to play around with it to your hearts content!

There's 2 versions: one is just the app/ folder, and the other contains the full CakePHP1.2 RC1 code as well:

I put some useful code in there to help create the database config file and set up the required tables, so just extract and play!

Comments (14):

  1. Bill said at 00:28, 7th July 2008:

    Hi Chris, the link DarkAuth.test.inc.cake.zip is off. Great component, thanks
  2. theChrisWalker said at 17:36, 7th July 2008:

    You're right, I mean to update it to the RC2 code anyway... I'll remove for now.
  3. Theo said at 21:30, 9th July 2008:

    Hi, is it not possible for you to add functions to save logins (user/groups) back to the HABTM ? for example a submitted form of username/password and checkboxes for groups? or is there just a easy way to do it in cakephp? thanks
  4. Theo said at 21:44, 9th July 2008:

    sorry i meant 'save logins (user/roles)' not 'save logins (user/groups)' and 'checkboxes for roles' not 'checkboxes for groups'
  5. theChrisWalker said at 09:17, 10th July 2008:

    "or is there just a easy way to do it in cakephp?" Bingo.
  6. itsnotvalid said at 13:02, 11th July 2008:

    Glad that the comment system is up now. I just leave a message here to hope to see a SVN/git repository or a single dark_auth.php download, instead of just hoping through the archive or copying from the website(where sensible person wouldn't do anyway). And this component is a great one and really save me a lot of time. Thank you!!
  7. Theo said at 12:59, 12th July 2008:

    had the wrong case on my models names in my form :/ thats why it wasnt working for me thanks anyways
  8. blablacio said at 15:15, 15th July 2008:

    Hello Chris, I'm developing an application which apparently needs user authentication and I chose your component to help out with this. Everything runs just fine now, but I was wondering if there is a way to do this: <?php class UsersController extends AppController { var $name = 'Users'; var $scaffold; var $_DarkAuth = array('required'=>'Administrators'); function register(){ } function add(){ } } ?> I want to let only users of the group Administrators to do anything within the UsersController, but let regular guests access the register() function only. Any ideas? Thank you for your time and exceptional component!
  9. itsnotvalid said at 12:08, 20th July 2008:

    Also I discovered a bug that, when you are having a login form for a action that also reads from $this->data, e.g. scaffold views (e.g. add), you would also submit empty dataset to those actions. In scaffold add, for example, you would create an empty record. How to fix this?
  10. theChrisWalker said at 20:48, 20th July 2008:

    @ blablacio: I emailed you about this, basically, there is no way "allow" specific actions as yet, but sounds like a good addition for the future.
  11. theChrisWalker said at 20:54, 20th July 2008:

    @itsnotvalid: after successful login, you are redirected using a GET request for the URL, all POST data will be lost - which means the Add scaffold should render a form, not insert a row. Still it will be easy enough to check for valid data before save()'ing the model. The loss of POST is not something easily avoided, so I hope you didn't plan to be able to propagate that.
  12. Lukas said at 16:11, 29th July 2008:

    Hello Chris, I have downloaded your script and I have protected a controller. The login-form appears, but if I submit my username and password, then I get the following error: Fatal error: Cannot use object of type PostsController as array in (...)app\controllers\components\dark_auth.php on line 341 Do you have any advice? thanks, Lukas.
  13. Lukas said at 16:22, 29th July 2008:

    dark_auth.php 341: $this->controller{$this->user_model_name}->belongsTo[$this->group_model_name]['foreignKey']; fix: $this->controller->{$this->user_model_name}->belongsTo[$this->group_model_name]['foreignKey']; Lukas.
  14. Darren said at 08:39, 5th August 2008:

    Great component Chris thanks, just wondering if it was possible to use the component in the cake error pages (404, missing action etc). I've not managed to do it yet. Cheers!

Leave a comment

More

...in the vault