Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Passing $URLSegment to PageController?


Go to End


5 Posts   11039 Views

Avatar
me.yay

Community Member, 14 Posts

18 February 2010 at 10:02pm

Edited: 18/02/2010 10:02pm

Hi,

i am kind of desperate.. I am unsing a Gallery and want do determine the filenames for the Images based on the $URLSegment.
The Pictures are located inside /themes/path/img/gallery and are named like the matching $URLSegment's with trailing numbers. The images are uploaded straight in the folder and not proccessed as assets.

My first approach was to pass $URLSegment within the control, which failed:
page.ss:

<% control GalleryImages($URLSegment) %>
    $Imagename
<% end_control %>

Then i found in the docs that $URLSegment should be an accessible variable inside the Page_Controller class (and subclasses).
This is my next approach:

page.php:

	function GalleryImages(){
	
		$path = "../themes/path/img/gallery";
		$filetypes = array("jpg", "png", "gif");
		
		$files = new array();
		$handle=opendir ($path);
		while ($file = readdir ($handle)) {
		  if (strlen($file) >= strlen($URLSegment)) {
			// filename starts with URLSegment?
		  	$matchfile= strpos($file, $URLSegment,0 ) === 0;
				// filename matches filetypes?
				foreach ($filetypes as $filetype){
					$matchfiletype=substr_compare($file, $filetype, ((int) strlen($filetype) * -1)) === 0;
					if ($matchfiletype && $matchfile) {
						$files[] = array('Image' => $file); 
					}
				}
			}
		}
		closedir($handle);
		return new DataObjectSet($files);
	}

What am i doing wrong? And how can i determin the current URLSegment inside the controller?

Kind Regards
Metin

Avatar
Willr

Forum Moderator, 5523 Posts

18 February 2010 at 10:45pm

how can i determin the current URLSegment inside the controller?

$this->URLSegment will return the controllers URL segment

Avatar
me.yay

Community Member, 14 Posts

19 February 2010 at 12:26am

Edited: 19/02/2010 12:26am

Thanks for the quick hint Willr!

It works like a charm :)

Avatar
merrick_sd

Community Member, 99 Posts

10 July 2014 at 11:24pm

I tried to make a quick enquiry widget.

The widget is on a page for example: http://mydomain/groups/cardiff

After saving the data and email i want to redirect to the same page.
however

$myredirector = Director::baseURL().Director::get_current_page()->URLSegment.'/?success=1';

Director::redirect($myredirector);
is returning

/cardiff/?success=1

i have tried
$myredirector = Director::baseURL(). $this->Link() . "/?success=1"
which returned
//groups/cardiff/widget/59/?success=1

i don't want the widget/59

Avatar
Devlin

Community Member, 344 Posts

10 July 2014 at 11:35pm

Edited: 10/07/2014 11:36pm

$myredirector = Director::get_current_page()->Link() . "?success=1";
// groups/cardiff/?success=1