2.3. Rewrite Rules

Recall that all Solar URIs are always in the form of controller/action/param/param/param. For example, in the demo blog app, the URI to read an article is blog/read/1. This is how Solar knows to route the URI request to the "blog" page controller, using the "read" action with a parameter of "1".

But what if you want to recognize URIs that have a different information architecture to them? This is where the dynamic rewrite rules come into play. You can use the rewrite rules here to convert incoming URIs not in that format to something that Solar will recognize internally.

There are two ways of expressing a rewrite rule: a short form, and a long form.

2.3.1. Short Form

In the short form, a rewrite rule is expressed as an array element, where the key is a regular expression patterns to match against incoming URIs. The element value indicates how to rewrite the matched URI. (These are used as the first two parameters to a preg_replace() call.)

Here is an example config file value for a short-form rewrite rule in the front controller:

// "blog/88/edit" gets rewritten as "blog/edit/88"
$pattern = 'blog/(\d+)/edit';
$rewrite = 'blog/edit/$1';
$config['Solar_Controller_Front']['rewrite'][$pattern] = $rewrite;

2.3.2. Replacement Tokens

You can also define replacement tokens for the regular expressions; this lets you predefine certain expression parts and reuse them in an easier-to-read way. The front controller defines a number of replacements, but you can add your own as well.

// Solar_Uri_Rewrite defines the following by default:
// '{:action}'     => '([a-z-]+)',
// '{:alpha}'      => '([a-zA-Z]+)',
// '{:alnum}'      => '([a-zA-Z0-9]+)',
// '{:controller}' => '([a-z-]+)',
// '{:digit}'      => '([0-9]+)',
// '{:param}'      => '([^/]+)',
// '{:params}'     => '(.*)',
// '{:slug}'       => '([a-zA-Z0-9-]+)',
// '{:word}'       => '([a-zA-Z0-9_]+)',

// you can add your own regex replacement strings as well
$config['Solar_Controller_Front']['replace'] = array(
    '{:product_slug}' => '(\d+\-[a-zA-Z]+)',

// "product/123-foobar/view" gets rewritten as "catalog/show-item/123-foobar"
$pattern = 'product/{:product_slug}/view';
$rewrite = 'catalog/show-item/$1';
$config['Solar_Controller_Front']['rewrite'][$pattern] = $rewrite;
[Note] Note

In a short-form rule, the replacement tokens have no semantic meaning and are not parameter keywords. They are used only as easy-to-read placeholders for reusable regular expression pieces.

2.3.3. Long Form

In the long form, a rewrite rule is still expressed as an array element. However, the element key is a name for the rule, and the element value is sub-array of processing instructions. We call this a "named action" or a "named rule" (in other frameworks this is called a "named route").

$name = 'blog-edit';

$rule = array(
    'pattern' => 'blog/{:id}/edit', // the URI pattern to match
    'rewrite' => 'blog/edit/$1',    // rewrite the URI like this
    'replace' => array(             // custom replacement tokens
        '{:id}' => '(\d+)',
    'default' => array(
        'id' => '88',

$config['Solar_Controller_Front']['rewrite'][$name] = $rule;

In your view scripts (described elsewhere) you can use a named action to generate a URI for you, with data interpolated into the URI.

// from inside a view script:
$name = 'blog-edit';
$data = array('id' => '70');
$text = 'Edit Blog Entry';
echo $this->namedAction($name, $data, $text);
// outputs <a href="/blog/70/edit">Edit Blog Entry</a>