Yii controller-action routes in Markdown

I’m using CMarkdown class to render Markdown-formatted texts stored in separate files (more details in this blog post). And I’ve run into issue with links in Markdown pointing to a different controller then current one.

This article provides some solution for this problem.

First, let me underline that we’re talking here about static Markdown text stored in some external file. If Markdown-formated text would be generated on-the-fly then there would be no problem at all, becase I could make use of CController::createUrl method.

Let’s go back to the core of this problem.

The problem

If I want to link to any action within the same controller, there is no problem:

Use [contact](contact.html) form.

But, when I want to point to a different controller, then this:

You must be [logged-in](user/login.html) to continue.

fails, because it adds controller (user) to the current one instead of replacing it, and generated URL becomes invalid.

On the other hand, if I use something like this:

You must be [logged-in](/user/login.html) to continue.

then CMarkdown creates a link with path (folder) cut-off, which — again — points to an non-existing action (invalid route).

And to avoid this, I have to use full application path:

You must be [logged-in](/yii/acrid_barebone/user/login.html) to continue.

Which I personally would like to avoid (for an obvious reasons).

The solution

Because CMarkdown is based on non-Yii parser, it is not controller-aware. And that’s why it works like shown.

To solve problem described in this article, you should try to use relative paths, i.e.:

You must be [logged-in](../user/login.html) to continue.

I was inspired by this Yii forum post.

Leave a Reply