register route not working

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
Post Reply
piotrekkr
Forum Members
Forum Members
Posts: 38
Joined: Mon Mar 05, 2007 5:30 pm

register route not working

Post by piotrekkr »

Why this routes:

Code: Select all

$this->RegisterRoute('/galeria-zwyciezcow(\/(?P<page>[0-9]+))?$/', array('returnid' => 28, 'action' => 'winner_gallery', 'inline' => 1));
or

Code: Select all

$this->RegisterRoute('/galeria-zwyciezcow\/(?P<page>[0-9]+)?$/', array('returnid' => 28, 'action' => 'winner_gallery', 'inline' => 1));
aren't working for links like:

Code: Select all

http://domain.com/galeria-zwyciezcow 
http://domain.com/galeria-zwyciezcow/
but works for link:

Code: Select all

http://domain.com/galeria-zwyciezcow/1
Any ideas? Thanks for help :)
User avatar
plger
Forum Members
Forum Members
Posts: 196
Joined: Wed Oct 15, 2008 10:38 am

Re: register route not working

Post by plger »

(Off the top of my head) Have you tried

Code: Select all

'/galeria-zwyciezcow\/(?P<page>[0-9]+?)$/'
?
piotrekkr
Forum Members
Forum Members
Posts: 38
Joined: Mon Mar 05, 2007 5:30 pm

Re: register route not working

Post by piotrekkr »

adding "?" after "+" only makes "+" ungreedy so it doesn't solve problem :(
piotrekkr
Forum Members
Forum Members
Posts: 38
Joined: Mon Mar 05, 2007 5:30 pm

Re: register route not working

Post by piotrekkr »

Ok I found problem... Problem is in index.php in line like this:

Code: Select all

//See if our page matches any predefined routes
$page = rtrim($page, '/');
//See if our page matches any predefined routes
$page = rtrim($page, '/');
if (strpos($page, '/') !== FALSE)
{

	$routes =& $gCms->variables['routes'];
	
	$matched = false;
	foreach ($routes as $route)
	{
		$matches = array();
		if (preg_match($route->regex, $page, $matches))
		{
			//Now setup some assumptions
			if (!isset($matches['id']))
				$matches['id'] = 'cntnt01';
			if (!isset($matches['action']))
				$matches['action'] = 'defaulturl';
			if (!isset($matches['inline']))
				$matches['inline'] = 0;
			if (!isset($matches['returnid']))
				$matches['returnid'] = ''; #Look for default page
			if (!isset($matches['module']))
				$matches['module'] = $route->module;

			//Get rid of numeric matches
			foreach ($matches as $key=>$val)
			{
				if (is_int($key))
				{
					unset($matches[$key]);
				}
				else
				{
					if ($key != 'id')
						$_REQUEST[$matches['id'] . $key] = $val;
				}
			}

			//Now set any defaults that might not have been in the url
			if (isset($route->defaults) && count($route->defaults) > 0)
			{
				foreach ($route->defaults as $key=>$val)
				{
					$_REQUEST[$matches['id'] . $key] = $val;
					if (array_key_exists($key, $matches))
					{ 
						$matches[$key] = $val;
					}
				}
			}

			//Get a decent returnid
			if ($matches['returnid'] == '') {
				global $gCms;
				$contentops =& $gCms->GetContentOperations();
				$matches['returnid'] = $contentops->GetDefaultPageID();
			}

			$_REQUEST['mact'] = $matches['module'] . ',' . $matches['id'] . ',' . $matches['action'] . ',' . $matches['inline'];

			$page = $matches['returnid'];
			$smarty->id = $matches['id'];

			$matched = true;
		}
	}

	if (!$matched)
	{
		$page = substr($page, strrpos($page, '/') + 1);
	}
}
When I add route like:

Code: Select all

'/galeria-zwyciezcow\/(?P<page>[0-9]+)?$/'
or
'/gallery\/?'
or anything that do not need '/' between start and end of route
and request 'http://host.com/gallery/' or 'http://host.com/gallery' it won't work.
IMHO it's bug.
.htaccess rewrite this to http://host.com?page=gallery/
next in index.php it is right trimmed so

Code: Select all

page=gallery
next

Code: Select all

if (strpos($page, '/') !== FALSE)
is always false so routes are ommited...
I think routes should be checked always first (regardless if there is '/' or not) before checking a page alias etc.
User avatar
duclet
Forum Members
Forum Members
Posts: 187
Joined: Fri Jun 23, 2006 12:55 pm

Re: register route not working

Post by duclet »

The problem that it is not working is because of the + which means it requires 1 or more. If you don't need it, it should be *. Though last I checked, it might need to be *+.
piotrekkr
Forum Members
Forum Members
Posts: 38
Joined: Mon Mar 05, 2007 5:30 pm

Re: register route not working

Post by piotrekkr »

No i tried '*' etc. Problem isn't there. Try make route that not need to have '/' within. For example:

Code: Select all

$this->RegisterRoute('/archive\/?$/', array('returnid' => 28, 'action' => 'archive', 'inline' => 1));
or
$this->RegisterRoute('/profile$/', array('returnid' => 28, 'action' => 'archive', 'inline' => 1));
or any route that not need '/' between start and end.
won't work but this will:

Code: Select all

$this->RegisterRoute('/profile\/1$/', array('returnid' => 28, 'action' => 'archive', 'inline' => 1))
or
$this->RegisterRoute('/profile\/something-after-slash$/', array('returnid' => 28, 'action' => 'archive', 'inline' => 1))
Post Reply

Return to “Developers Discussion”