How to make dynamic pretty urls

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Locked
cve
Forum Members
Forum Members
Posts: 44
Joined: Wed Jul 07, 2010 10:54 am

How to make dynamic pretty urls

Post by cve »

My module has categories and products. I want to have pretty links everywhere. I want to show details of each category, so I've made action.categories_list.php file which content is below:

Code: Select all

<?php

/* @var $cms Shop */
$cms = $this;

$templateName = !empty($params['template']) ? $params['template'] : null;
$categories = $cms->getCategories();

foreach ($categories as &$category) {
      $category['link'] = $cms->_createFront('category_details', $id, $returnid, array('category_id' => $category['id']), false, $cms->_url_prefix . $category['slug']);
}

$this->smarty->assign('categories', $categories);
$template = $cms->getTemplateByTitle($templateName, 'categories_templates_tab');
if (!$template) {
    $template = $cms->getDefaultTemplateForType('categories_templates_tab');
}

echo $cms->ProcessTemplateFromData($template['template']);
which outputs:

Code: Select all

<section>
<a href="http://www.faab.local.pl/shop/bart-czyz/" title="Bart Czyz">Bart Czyz</a>
<a href="http://www.faab.local.pl/shop/jan-kowalski-local/" title="Jan Kowalski local">Jan Kowalski local</a>
</section>
this works fine.

The next step is makes routes for this categories, so just like in News.module.php i wrote that code in InitializeFrontend of my module

Code: Select all

$categoriesSlugs = $this->getCategoriesSlugs();
        $productsSlugs = $this->getProductsSlugs();

        $gCms = cmsms();

        $contentops = $gCms->GetContentOperations();
        $category_page = $contentops->GetDefaultContent();

        if (!empty($categoriesSlugs)) {

            foreach ($categoriesSlugs as $category) {
                $parms = array(
                    'action' => 'category_details',
                    'returnid' => $category_page,
                    'category_id' => $category['id']
                );

                $condition = (!empty($category['url'])) ? $category['url'] : $category['slug'];
//$this->_url_prefix contains 'shop/'
                $urlString = $this->_url_prefix . $condition;

                $route = new CmsRoute($urlString, $this->GetName(), $parms, TRUE);
                debug_display($urlString);
                cms_route_manager::register($route);
            }

        }
So this snippet of code register my routes as such:

Code: Select all

Debug: (0.163034) - (usage: 5178944) - (peak: 5219504)
CmsRoute Object
(
    [_dest:CmsRoute:private] => Shop
    [_content_id:CmsRoute:private] => 
    [_term:CmsRoute:private] => shop/bart-czyz
    [_defaults:CmsRoute:private] => Array
        (
            [action] => category_details
            [returnid] => 
            [category_id] => 4
        )

    [_absolute:CmsRoute:private] => 1
    [_results:CmsRoute:private] => 
)
Debug: (0.16325) - (usage: 5180260) - (peak: 5236284)
CmsRoute Object
(
    [_dest:CmsRoute:private] => Shop
    [_content_id:CmsRoute:private] => 
    [_term:CmsRoute:private] => shop/jan-kowalski-local
    [_defaults:CmsRoute:private] => Array
        (
            [action] => category_details
            [returnid] => 
            [category_id] => 5
        )

    [_absolute:CmsRoute:private] => 1
    [_results:CmsRoute:private] => 
)
THE PROBLEM IS when I click the category link the cms shows me up the one of the default pages in cms made simple, but I want to show the action called "category_details"... I'm so confused of that... What's going wrong? Can someone help me?
cve
Forum Members
Forum Members
Posts: 44
Joined: Wed Jul 07, 2010 10:54 am

Re: How to make dynamic pretty urls

Post by cve »

I just solved the problem with add new page (hidden in menu) that is the target for my "category_details" action, and fetch the content id of that for for sets it to "returnid" of my categories routes... and it works, but I think this is not elegant solution...
bbonora
Forum Members
Forum Members
Posts: 48
Joined: Mon Nov 06, 2006 6:10 am

Re: How to make dynamic pretty urls

Post by bbonora »

I was having this same issue last night. The way I finally solved it was to move all the route information from the InitializeFrontend() method to the CreateStaticRoutes() method. Just for clarification this code is located in the MyModule.module.php file.

After I moved my code around, I went to Site Admin -> System Maintenance -> Cache and Content and clicked on the update static routes button. Once that was complete I was able to navigate to the route on the front end and have it display the action file from my module - in my case this was the action.detail.php file.

Below is an example of my route registration code before and after I got it to work.

Code: Select all

// BEFORE
public function InitializeFrontend()
{
    $parms = array('action'=>'detial','another_param'=>...);
    $r = '/mymodule\/(P?<some_parameter>[0-9]+)\/...';
    $route = new CmsRoute($r,$this->GetName(),$parms);
    cms_route_manager::add_static($route);
}

// AFTER
public function RegisterStaticRoutes()
{
    $parms = array('action'=>'detial','another_param'=>...);
    $r = '/mymodule\/(P?<some_parameter>[0-9]+)\/...';
    $route = new CmsRoute($r,$this->GetName(),$parms);
     cms_route_manager::add_static($route);
}
Prior to the above mentioned action, I was getting the same result that you got, which was the page would display fine but it would redirect to the page with the specified page id in the static route. For example I was trying to create a route like this - http://mydomain.com/mymodule/contestant/3/. Before I got this to work it would display the page with an id of 3.

I was hoping that someone with a little more knowledge of the core could shed some light on this topic. Specifically, I'm wanting to learn more about the methods below. I would like to know when they are loaded and what the intended purpose is as well as what the appropriate usage is - (best practices).

I know that you should register most routes in the install file but what if you need to register routes on the fly or discover a need for a route after the module tables have already been populated with data? Is this still possible? I tested out some different scenorios last night and was not able to achieve the creation of new routes. The only way I could get it to work was to uninstall and then re-install the entire module or to use the update static route button.

Code: Select all

InitializeFrontend()
InitializeAdmin()
LazyLoadFrontend()
RegisterStaticRoutes()

// I've seen the combination all of these used 
// but it's unclear to me when and where they should be used
CmsRoute()
CmsRoute::new_builder()
cms_route_manager::add_dynmic
cms_route_manager::add_static
cms_route_manager::register
Locked

Return to “Developers Discussion”