Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
Hello !

I'm looking for a cms for updating my website and CouchCMS is one among the ones I like.
I have read the doc of course and have pendant questions.
First one is related to url and pretty url native process.

In my case, I dont like "sluged" url for main entries (acceptable for categories and tags though) : some of my main titles are quite long, some are not unique and I dont feel good with verbose url. I prefer a numeric "id". What makes sense and what should catch the search engine at the end is the content.

Can someone better skilled than me here tell if this (using id for pretty url) is (easily) possible ? and how to get it done ? It looks like routage is something the user can not drive in couchcms, as of now at least.
If not possible, is there any workaround ?
What happens if one gives the title (wich is used for processing the pretty url isn'it ?) a number (an id), then use another text field for the real title ? or hack the pretty name function so that it would make use of the id instead of the name ?
Isn't it possible to define a new case (define( 'K_PRETTY_URLS', 2); with a new set of naming rules ? (using id rather than creating slug from name) ?

That's my first post. Please don't find any criticism here ; couchcms looks good ;)
Hello, @sellig, and welcome to the Couch community!

Like you, I'm a newbie, so you may end up getting different (better) answers from the more experienced Couch users here, but here is my suggestion.

As you mentioned yourself, Couch already has the mechanisms for doing what you want: it does pretty URLs, and it does give pages an automatic ID. Where the problem comes in, in your situation, is that you want Couch to automatically assign a unique ID to the page's title (quite easy to do) - but also, and most importantly, not give the user the option of entering a title of their own.

The good news is: Couch can do this already! Have a look at the "Databound Forms" tutorial, here:

http://www.couchcms.com/docs/concepts/d ... forms.html

This tutorial is designed to show you how to:

1) create totally customised forms on the frontend that directly save data to the backend - so (some of) your users do not have to login to the backend at all (not something you necessarily need in your situation), and

2) how to totally customise the backend, using your own list and single-entry layouts (which is what you need).

In the example used in that tutorial, a user posts a job application from the frontend, and the job application is automatically assigned a unique ID (not a slug) for its 'title'. It then shows you how to customise the backend, to show appropriate list and entry forms that don't make use of the numerical title.

So, if you follow this tutorial, by the end of it, you should be able to do what you want.

I hope this helps!
Hello and welcome, sellig :)

As suggested by @luxlogica, using databound form could be one solution.

The best solution, however, would have been if we could define our own Rewrite rules so as to create URLs of our own making.

As things stand right now, the rewrite rules are pretty much baked into the system and cannot be modified from within.

However, we can hack around this with a little effort.

As an example, I'll assume it is the 'blog.php' template that needs new kind of URLs for the page-view.
With prettyURLs on, the default URL a blog page would get could be
http://localhost/blog/my-first-post.html

or the following if folders are used -
http://localhost/blog/one/two/three/my-first-post.html

Instead of the 'page_name' in the URLs above ('my-first-post'), we'll now try and put the page_id instead so the URL would become
http://localhost/blog/34.html
http://localhost/blog/one/two/three/34.html

The process will require 3 steps -
1. The k_page_link variable cannot be used as is. It now needs to show our modified URL (id instead of page). For this place the following code in 'couch/addons/kfunctions.php' file (rename the 'kfunctions.example.php' to 'kfunctions.php' if needed) -
Code: Select all
// Register 'cms:my_link tag' (replaces page_name with page_id.
$FUNCS->register_tag( 'my_link', 'my_link_handler' );
function my_link_handler( $params, $node ){
    global $CTX, $FUNCS;
   
    extract( $FUNCS->get_named_vars(
        array(
            'link'=>'',
        ),
        $params)
    );
    $link = trim( $link );
   
    // replace page_name with page_id in the link
    if( K_PRETTY_URLS && $CTX->get('k_is_page') ){
        $page_id = $CTX->get('k_page_id');
       
        $pattern = '#(.*?)([^\.\/]*)\.html$#i';
        $replacement = '${1}'.$page_id.'.html';
        $link = @preg_replace( $pattern, $replacement, $link );
    }
   
    return $link;
}

The code above creates a new tag named 'cms:my_link'.
Given a page's canonical URL (i.e. with the page_name) as its parameter, it returns a URL with the name replaced by the page's ID.
e.g.
<a href="<cms:my_link k_page_link />"><cms:show k_page_title /></a>


2. With the new URL in place, we now need to modify our .htaccess file to recognize them.
In the generated .htaccess, find the the default rule for 'blog.php' that handles the page_view i.e. the following
RewriteRule ^blog/.*?([^\.\/]*)\.html$ blog.php?pname=$1 [L,QSA]

and modify it to make it -
RewriteRule ^blog/.*?([^\.\/]*)\.html$ blog.php?p=$1 [L,QSA]

Notice how we are changing the pname to 'p'.

3. Finally, one last change needs to be made to get around the following behaviour of Couch - whenever Couch senses that a page has been accessed with a URL that does not match the default canonical URL, it redirects the user to the canonical URL (as hard-coded in Couch).

To turn this behaviour off, edit your blog.php template to modify the last PHP statement form
<?php COUCH::invoke(); ?>

to the following
<?php COUCH::invoke(1); ?>
In case you didn't notice the change, we have placed a '1' within the parenthesis.

That should be it.
Your blog pages should now be accessible with the new ID urls. Try testing them -
Code: Select all
<ul>
<cms:pages>
    <li><a href="<cms:my_link k_page_link />"><cms:show k_page_title /></a></li>
</cms:pages>
</ul>

The method outlined above can be extended to create URLs of any types (provided one knows his way around mod_rewrite rules).

Hopefully sometime in the future Couch would allow specifying custom rules for templates. That would avoid all the effort we had to make above to get URLs of our liking.

Hope this helps.
Do let us know.

Thanks.
Thank you luxlogica and KK for your helpfull and friendly answers. I'm going to play with your code and your suggestions soon, while learning how to make friend with Couch. Firstly, sure, I need to understand the basics and play more.
4 posts Page 1 of 1
cron