Problems, need help? Have a tip or advice? Post it here.
7 posts Page 1 of 1
I have been working on a project but i have hit a snag with custom routes and parameters.
My code is as follows:
Code: Select all
.....
<cms:route name='home' path='' />

<cms:route name='page' path='{:page_name}'  filters='page_exists=name'>
        <cms:route_validators
                page_name='title_ready' />
        <cms:route_validators
                id='non_zero_integer' />
</cms:route>

<cms:route name='folder' path='{:folder_name}' filters='page_exists=name'>
        <cms:route_validators
                page_name='title_ready' />
        <cms:route_validators
                id='non_zero_integer' />
</cms:route>

<cms:route name='parameters' path='{:folder_name} {:page_name}' filters='page_exists=name'>
         <cms:route_validators
                 page_name='title_ready' />
         <cms:route_validators
                 id='non_zero_integer'/>
</cms:route>

</cms:template>

<cms:match_route debug='1' is_404='1' />
<cms:if k_matched_route='page' >
   <cms:embed 'page' />
<cms:else_if k_matched_route='folder' />
   <cms:embed 'folder' />
<cms:else_if k_matched_route='home' />
   <cms:embed 'default' />
<cms:else_if k_matched_route='parameters' />
   <cms:embed 'parameters' />
</cms:if>


Only HOME and Page return a k_matched_route the rest fail. The link returns nothing. May be it is because i am trying to populate the parameters dynamically from a different template. The links invoking the view are as follows:

HOME:
template-1.php
----------------
pretty url should return-(www.localhost.com/template)

PAGE: (template-1.php page view)
<cms:route_link 'page' rt_page_name=k_page_name />">
---------------
pretty url should return-(www.localhost.com/template/page-name.html)

FOLDER: (template-1.php folder view)
<cms:route_link 'folder' rt_folder_name=k_page_foldername />">
---------------
pretty url should return-(www.localhost.com/template/foldername)

PARAMETER: (template-1php page and folder view but with additional page and folder variables from template-2.php) - from template-2.php i fetch custom_field from pages and use it to pupulate the page_name and Folder_name variables then send them with route_link to template-1, thus:

<cms:route_link 'parameter' rt_page_name=k_page_foldername />">
where k_page_foldername is now template 2's folder name and k_page_name is now template 2's page name.

---------------
pretty url should return-(www.localhost.com/template/page-name-page-name.html) and or
---------------
pretty url should return-(www.localhost.com/template/folder-page-name.html)

++++++++++++

Kindly assist me with a cleaner simpler way of doing this as i have yet to master custom routes.

As always i appreciate your help.
Hi,

I think you should define the routes as follows -
Code: Select all
    ..
   
    <!-- define routes -->

    <!-- www.localhost.com/template -->
    <cms:route name='home' path='' />

    <!-- www.localhost.com/template/page-name.html -->
    <cms:route name='page' path='{:page_name}{:format}' filters='page_exists'>
        <cms:route_constraints  format='(\.html)' />
        <cms:route_validators
            page_name='title_ready'
        />
    </cms:route>

    <!-- www.localhost.com/template/foldername -->
    <cms:route name='folder' path='{:folder_name}' filters='folder_exists'>
        <cms:route_validators
            folder_name='title_ready'
        />
    </cms:route>

    <!-- www.localhost.com/template/foldername/page-name.html -->
    <cms:route name='parameters' path='{:folder_name}/{:page_name}{:format}' filters='folder_exists | page_exists'>
        <cms:route_constraints  format='(\.html)' />
        <cms:route_validators
            folder_name='title_ready'
            page_name='title_ready'
        />
    </cms:route>

</cms:template>

Set debug='1' for cms:match_route to check how the routes match.

The 'parameters' route you want will not work the way you envisaged because then its URLs will be identical to 'page' route (i.e. the two routes will become ambiguous). So you need to separate the 'folder_name' and 'page_name' with a '/'.

Does this help?
Thank you KK for your help.

Your suggestion works and i was able to achieve the solution. I would really like to master couch; it is such a simple but powerful tool for web designers.
Hi KK,

I spoke too soon!!!!

I have not been able to achieve what i wanted and i really needed to have the site published so i opted to stick to what i know and rectify the issues later. Since i have the site published maybe you can be able to help me get it right. I have stuck to add_query.

In brief:
My primary url is:
http://www.hireddrive.com/cars/
This Url has 3 views as usual using couch's page-view, folder-view and home-view

I invoke other views based on the following urls:
http://www.hireddrive.com/locations/
http://www.hireddrive.com/airport/

The views are simply the folder and page views of the cars template but now with additional information of the folder-view and page-views of the airport.php template and the locations.php templates.

For instance:
locations/folder has a link containing cars/folder and cars/page the location/foldername and location/pagename is the variable that i pass to cars/folder using <cms:add_query> at the locations page and <cms:if> at the cars/folders and cars/page to get it to display as follows:

http://www.hireddrive.com/cars/4x4/?region=kisumu
which will contains a link that will invoke page-view:
http://www.hireddrive.com/cars/4x4/land-cruiser-j70.html?region=kisumu
http://www.hireddrive.com/cars/4x4/?location=kisumu-museum

All i want to now do is get rid of ?region= and ?location=

I have tried .htaccess RewriteRule rules under the car template but that just ends up breaking the entire site.

I have also tried to use nested pages -masquerade- but even with the new page name the "?region=", still gets repeated. So my only solution for clean url structure is the custom routes. However when i implement the solution you gave i do get the page-view, folder-view and home-view of the cars.php but when i pass variables from locations.php this does not work. (just like you posit in "two routes will become ambiguous".)

I am stuck. I need the pretty urls as it is very important for this site.

In addition, i would like to remove the .html extension on HTACCESS but the file generated is explicit #DO NOT EDIT BELOW THIS.

Your advice and help is appreciated.
Hi,

You asked for my advice so here it goes -

I had a look at the site and (regarding the cars.php template) it seems that the regular page-view and folder-views are working well for you.

If now you decide to use custom routes for the cars template, please keep in mind that the page-view and folder-view will no longer be available. Instead you'll have to define their routes yourself and also generate the relevant links manually (i.e. instead of simply using k_page_link, you'll have to use cms:route_link with the required parameters to generate the links to pages/folders).

The site seems to be working fine except that some urls have ?region= and ?location= appended to them. I doubt if the kind of rework you'll have to do at this stage just to remove those would be justified.

That said, it is your call finally.
Please think over it and let me know.

Thanks.
Thanks KK for your response.
I thought about it and i initially wanted to let it be. But when i tried to generate a sitemap, all the pages with ?region= and ?location= fail. So it seems like i need either a .htaccess solution that will get rid of ?region= and ?location= or a custom routes solution.

I have been cracking my head on the custom routes solution and i still cannot be able to generate the cars.php folder-view and page-view that i want to invoke dynamically from the folder-view and page-view of the locations and airports templates. If i can get the .htaccess shortcut the better as i try to learn more and exploit the power of custom routes.

Is there a .htaccess solution for this part of the htaccess?

Code: Select all
#cars.php
RewriteRule ^cars$ "$0/" [R=301,L,QSA]
RewriteRule ^cars/$ cars.php [L,QSA]
RewriteRule ^cars/.*?([^\.\/]*)\.html$ cars.php?pname=$1 [L,QSA]
RewriteRule ^cars/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ cars.php?d=$1$2$3 [L,QSA]
RewriteRule ^cars/[^\.]*?([^/\.]*)/$ cars.php?fname=$1 [L,QSA]
RewriteRule ^cars/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]
I have chosen custom routes as the path i want to take. This is because generated variables are not getting filtered; any variable entered after region/location get executed.

I have done some work on the custom routes as follows but only home returns true.

Code: Select all
//DEFINE ROUTES
===============
<!--Default View-->
<cms:route name='home' path='' />

<!--page-view-->
<cms:route name='page' path='{:page_name}{:format}' filters='page_exists'>
  <cms:route_constraints  format='(\.html)' />
  <cms:route_validators
      page_name='title_ready'
  />
</cms:route>

<!--folder-view-->
<cms:route name='folder' path='{:folder_name}' filters='folder_exists'>
    <cms:route_validators
        folder_name='title_ready'
    />
</cms:route>

//this view is invoked by locations and airport template
<cms:route name='service-region' path='{:service_name}/{:folder_name}/' filters='folder_exists | page_exists'>
    <cms:route_constraints  format='(\.html)' />
    <cms:route_validators
        folder_name='title_ready'
        page_name='title_ready'
    />
</cms:route>

//when the service-location view is invoked it can invoke its own page views
<!--service-page-view-->
<cms:route name='service-location' path='{:service_name}/{:folder_name}/{:page_name}{:format}' filters='folder_exists | page_exists'>
    <cms:route_constraints  format='(\.html)' />
    <cms:route_validators
        folder_name='title_ready'
        page_name='title_ready'
    />
</cms:route>


I have tried to run this but only home matches successfully.

Any help will be greatly appreciated.
7 posts Page 1 of 1
cron