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.

All other Modules /

Discuss all other Modules here.

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

URL mapping for Module


Go to End


5 Posts   941 Views

Avatar
Webko

Community Member, 3 Posts

20 June 2012 at 5:53pm

Dear All,

Let's say I have a module GadgetSearch, with a page type in the CMS. GadgetSearch uses an XML call to gather it's data - there is no relevant database table for the search.

All is good andworks great, so long as I call /gadgetsearch/ and/or remember to change the MetaData URL entry to read /gadgetsearch/

But if I create a new page called Gadget Search, then the URL will be automatically /gadget-search/ and this fails. Is there a way to use the CMS or the controller to go to the right place? I know I could edit the config for the module, but the users can't do this...

Cheers

T

Avatar
Willr

Forum Moderator, 5523 Posts

20 June 2012 at 7:48pm

Were have you hard coded the URL? You should use the Link() function rather than hardcoding any page URL. You could also setup a route to point a URL to a given controller (just like googlesitemaps works - https://github.com/silverstripe-labs/silverstripe-googlesitemaps/blob/0.2/_config.php#L4)

Avatar
Webko

Community Member, 3 Posts

21 June 2012 at 11:33am

What I mean is the module lives at /gadgetsearch/

If an admin user uses the Gadget Search page type to create a new top level page, the navigation will go to wherever they specified in the Page Name by default, ie if they decide to call the page Widget Search, then by default this will go to /widget-search/ on the site from the navigation.

As I can't tell what they might call the page, how can I make sure a page of the type Gadget Search always ends up in the module folder /gadgetsearch/ ?

Avatar
Willr

Forum Moderator, 5523 Posts

21 June 2012 at 6:15pm

You can't always make sure. I think what you want is when the user goes to a page something happens so you would have a page type setup like

class GadgetSearchPage extends Page {

}

class GadgetSearchPage_Controller extends Page_Controller {

}

If you want to perform an operation when the user hits a GadgetSearchPage then you can include something in the init() function of the Controller

class GadgetSearchPage_Controller extends Page_Controller {

function init() {
echo "Hi!";
}
}

Whenever you hit a page type of 'GadgetSearchPage' then it will echo 'hi'. I.e if you create a GadgetSearchPage in the CMS and call it 'hello' then site.com/hello will echo 'hi'.

Avatar
Webko

Community Member, 3 Posts

22 June 2012 at 11:19am

*head desk*

That showed it was being called correctly, but the controller was incorrectly named...

*sigh*

Works a treat now, strangely...