Skip to content
John Loehrer edited this page Sep 6, 2013 · 3 revisions

ShortCircuit Framework

Wukka's MVC framework tries to keep a tiny footprint and allow you to just build your application. Rather than spending hours manipulating cludgy namespaced classes for controllers and views, we rely on a convention of php includes in a directory structure that reflects a canonical url path. Action files house your controller logic and view files house your templating layer.

Build your first app

create an entry point

You need is an entry point in a php web server public directory for your application. Create a file called shortcircuit.php in your web root directory. (We'll get fancy with clean urls later).

<?php
include '/path/to/autoload.php';
use Wukka\ShortCircuit;
ShortCircuit::setAppDir('/path/to/my/application/');
ShortCircuit::run();

Make sure you put the library outside of your web root directory. The same goes for your application code. Set up a new folder outside the webroot for your application files.

create an index action

Let's build our first page. Go to the application directory you just created. create a file in it called index.action.php:

<?php
return array('message'=>'hello world');

This is the default action performed. Shortcircuit will go to this page if no url is specified.

Create an index view

Let's create a view to read it by creating a file in index.view.php:

<h1><?php echo $this->message; ?></h1>

Now when you browse to your entry point url /shortcircuit.php you should see the message in big bold letters "hello world".

Mapping urls to actions

To see how shortcircuit resolves urls to actions, let's set up another example. Create a file in test.action.php:

<?php return array();

Also create the view in test.view.php:

<h1>TEST</h1>

Then load the url /shortcircuit.php/test/. You should see the bold letters "TEST".

templating

create a new view file in your application directory called templates/message.view.php:

<div class="message"><?php echo $this->message; ?></div>

Now go back to index.view.php and change it to:

<?php $this->render('templates/message'); ?>

when you load the url /shortcircuit.php/ you should see the message "hello world" but wrapped up in a div instead of the previous h1 tags.

URL patterns

shortcircuit has a way for you to map urls to actions based on regex rules. let's go back to shortcircuit.php in our webroot and change it to:

<?php
include '/path/to/autoload.php';
use Wukka\ShortCircuit;
ShortCircuit::setAppDir('/path/to/my/application/');
ShortCircuit::resolver()->addURL('/foo/', 'test');
ShortCircuit::run();

This will wrap the default resolver we used before with a new smarter resolver, using the regex patterns we give it to try first.

Try loading the url /shortcircuit.php/foo/. You should see the bold letters "TEST". The resolver matched our url to the pattern /foo/ and dispatched the test action and view in response.

extract URL arguments into request

What if you want to pass an id via a url? Let's tweak our pattern in the shortcircuit.php entry point file a bit. Instead of a simple regex pattern directly mapped to 'test', we can specify a more complicated relationship:

ShortCircuit::resolver()->setURLS( array('foo'=>'test', '/foo/(id)/'=>'test') );

And let's go back and change the test.view.php file:

<h1>TEST</h1>
<p>id: <?php echo $this->request()->id; ?></p>

Now load the url in your browser /shortcircuit.php/foo/123/. You should see the words TEST followed by "id: 123". The pattern matched on foo, and extracted the id embedded into the url and put it into the request based on the name we gave it in params.

Creating a reverse link based on an action name

Let's use the regex pattern we supplied earlier to create a link to the app. We can do this without knowing what the url is in our application or how it is constructed. Edit test.view.php again:

<h1><?php echo $this->link('test', array('id'=>$this->request()->id) ); ?></h1>

When you reload your browser, you should see /shortcircuit.php/foo/123/ in big bold letters.

Put patterns

Where do models go?

Most MVCs provide a structure for where to put models or how to connect to the database. We assume you will choose what works best for you and build all of that on your own. A model is nothing more than a domain object that represents your business logic. Create an autoload hook for your model classes and then call them in your action. The action can either return data from the model to the view, or just return the model itself to the view and allow the view to consume the data.

Exception handling

Let's test out exception handling in shortcircuit. Edit test.action.php and put in the following code:

<?php
throw new Exception('woe!');

Now, let's set up a default error handler. Edit error.view.php:

<h1>Error: <?php echo $this->exception->getMessage(); ?></h1>

Now load the url /shortcircuit.php/test/. You should see this message in bold letters: "Error: woe!".

We can override this default error handler for our action. Create a new file testerror.view.php:

<p>Test Error</p>

Reload the page. You should see the words "Test error".

How exception handling works.

You can catch exceptions in your actions if you want and pass error information to your view. But shortcircuit has built in error handling as well. Any uncaught exceptions will be captured. The Router will then try to render a view for <action>error.view.php. For example if my action test.action.php calls code that throws an exception, shortcircuit would look for a view at testerror.view.php. If the error view isn't found, it will go to the default error view for your application, error.view.php. This gives you the opportunity to see if you want to display debug information or parse the error message and convert it into a user-friendly error message.