Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
@KK, I have studied all the advanced concepts by now, as our community project required, and now going through Advanced Tutorial. This Custom Routes addon is very nice and easy to implement.
What is the main difference between pushing url with gpc or custom route, in essense? Something, that can not be achieved the usual way, made possible only by routes?
KK wrote: For example, following could be a prettyURL that edits invoice no. 190 that resides in a folder of id 11 - Quote: http://www.yoursite.com/invoice/190/edit/11/folder/
Clearly, the default views of Couch won't be of much help in this situation.

What if a url would be like:
http://www.yoursite.com/invoice.php?folder=11&id=190&edit=1
With such url, after easy parsing with gpc we have usual k_is_home and data-bound-form stores input to page.
What do I miss here?
I'll try to answer.

If you don't want prettyURLs, then the 'gpc' method would work too.
However, you'll now lose out on the 'validators', 'constraints' and 'filters' that we can attach with custom-routes.

For example, suppose someone submits -
http://www.yoursite.com/invoice.php?fol ... 190&edit=1

Now after getting the values of 'folder', 'id' and 'edit' using cms:gpc, the next step would be to -
1. Check if 'edit' is numeric and is '1'
2. Check if 'id' is numeric. If yes, if a page with that id actually exists.
3. Check if 'folder' is numeric. If yes, if a folder with id actually exists. If yes, check if the page above actually belongs to this folder.
4. Perhaps also need to check if the user has the right permissions.

It is only after you have successfully validated *all* the above points that you actually get down to the real business of editing that particular page.

And, mind you, you'll have to do almost the same checks for all other URLs too.

Now consider what happens if we had used custom-routes.
If we had declared the right constaints, validators and filters, the following block of code would be executed *only* after all checks have passed -
Code: Select all
<cms:if k_matched_route='edit_view' />
     .. simply go ahead and edit the page .. all things are already validated   
</cms:if>

This results in a much more manageable and reusable code (all validators and filters can be shared across routes).

Custom-routes kind of mimic the traditional MVC architecture, where the snippets implementing the real action are the 'controllers'.
For even somewhat complex applications, this can be big help.

In closing, Couch (true to its principle) does not force you into using anything.
Feel free to use whatever technique suits you best :)
Adding to my last post -
You asked
Something, that can not be achieved the usual way, made possible only by routes?
Nothing at all (discounting prettyURLs). Everything custom-routes do, can be achieved using plain cms:gpc but would likely result in very convoluted and hard to manage code.

It is exactly what could be said about traditional MVC applications. Everything they do can be done using straight procedural PHP but that usually results in code that only the original developer can read (for an year or so :) )
Very helpful!
Everything custom-routes do, can be achieved using plain cms:gpc

I'm glad to know that there is nothing I miss out using the traditional ways.

Edit: we know that Couch is a higher level of abstracttion over php. So custom route - is now an abstraction over Couch itself, lol. Gr8!
4 posts Page 1 of 1