How to implement content page serving?

Hello everyone,

First I would like to say "thank you" for such a nice framework. I've been playing around for a while with the yii, and I've got pretty common question that I didn't find the response for.

I have the application, that has static pages except of functional. I mean the pages that have only content area changed through the administrative area. Each of these pages has an unique URI identifier. I.e. "terms-conditions", "about-us", etc.

How could I serve them from my application? There isn't any pre-defined list of these pages and there URI may be changed in future via the administrative area.



This is what a typical CMS should do. Using Yii, you could do the following to implement what you want:

  1. Store your content in a database table. Each content should be uniquely identified by something (an integer ID, the title, etc.)

  2. Create a ContentController and a view action inside it. The view action is responsible to parse the URL and fetch the corresponding content from the database table (you can use ActiveRecord to achieve it easily).

  3. Create an action view which renders the fetched content.

Now your URL may look like: /index.php?r=content/view&title=terms-conditions

To make it pretty, such as /content/terms-conditions, you can use Apache rewrite rules, or use CUrlManager provided in Yii.

Thanks for such a quick response. That will work.

–To make it pretty, such as /content/terms-conditions, you can use Apache rewrite rules, or use CUrlManager provided in Yii.–

What if we wanted to make it one step prettier:

such as:

How can we dynamically achieve this using CUrlManager regular exp​ression routing?

With the upcoming 1.0.3, you could use the following rule:


What if we had 1000 pages, then would we need 1000 rules?

Why not something like '(.*)' => 'content/view/title/{1}'

Yes, you can use the following then,


You can also use Apache rewrite rules to achieve the same goal.

Note, however, that CUrlManager manages URLs in two directions, i.e., parsing URLs and creating URLs. Apache rewrite rules (and the routing system in many other PHP frameworks) work only in one direction, i.e., parsing URLs.

Would prefer keep everything inside Yii =).

Thanks for quick reply!  Except now I can't access my modules following your suggestion.

What's the order of routes to be resolved?  Would it make sense to serve up modules first by default before going to rules?

Then a final catch all, in this case '<title:.*>'=>'content/view' would default to loading up pages from the db based on the unique title.

The rules are evaluated in the order they are listed.

Because this rule matches everything, it effectively disables access to any other features. Therefore, in front of this rule, you should have some other rules.