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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

SilverStripe Routes - Do they work?


Go to End


7 Posts   3541 Views

Avatar
mediabeastnz

Community Member, 10 Posts

4 December 2014 at 5:12pm

Edited: 04/12/2014 7:34pm

I'm not sure if I'm just missing something with SS routes but I can't seem to get any working using 3.1.7.

I have a page controller "AccountPage_Controller" that extends "Page_Controller".
All I want to do is have a couple routes on this page e.g. "http://localhost/website/account/address/deleteAddress/1".

I literally copied the same example from the docs with out any luck. See my code below and the results when executed.

class AccountPage_Controller extends Page_Controller {
	
	private static $allowed_actions = array (
		'address'
	);
	
	private static $url_handlers = array(
		'address/$Action/$ID' => 'deleteAddress'
	);

        public function deleteAddress(){
		return true;
        }
}	

Results in (using debug_request):

Debug (line 250 of RequestHandler.php): Testing 'address/$Action/$ID' with 'address/deleteAddress/1' on AccountPage_Controller
Debug (line 258 of RequestHandler.php): Rule 'address/$Action/$ID' matched to action '$Action' on AccountPage_Controller. Latest request params: array ( 'Action' => 'deleteAddress', 'ID' => 1, )
Debug (line 250 of RequestHandler.php): Testing '$Action//$ID/$OtherID' with '' on ErrorPage_Controller
Debug (line 258 of RequestHandler.php): Rule '$Action//$ID/$OtherID' matched to action 'handleAction' on ErrorPage_Controller. Latest request params: array ( 'Action' => NULL, 'ID' => NULL, 'OtherID' => NULL, )
Debug (line 184 of RequestHandler.php): Action not set; using default action method name 'index'
Debug (line 157 of Controller.php): Request handler returned SS_HTTPResponse object to AccountPage_Controller controller;returning it without modification.

[User Warning] Couldn't set response type to 404 because of output on line 165 of /Users/USER/Sites/website/framework/dev/Debug.php
Sorry, it seems you were trying to access a page that doesn't exist.

And that's with a dev/build, dev/build?flush=all, dev/build?flush=1

Any ideas?

Avatar
nzstephenf

Community Member, 63 Posts

4 December 2014 at 5:33pm

Hey man,
Is this a direct copy and paste?

If so - your code should be:

class AccountPage_Controller extends Page_Controller { 
    private static $allowed_actions = array( 
        'address' 
    ); 
    
    private static $url_handlers = array( 
        'address/$Action/$ID' => 'deleteAddress' 
    );

    public function deleteAddress(){
        return true; 
    }

}   

Other than that, I could be absolutely-wrong but I don't think you need to use $url_handlers anymore. Someone prove me wrong! Because then I can try using $url_handlers. I read a blog article on a different site stepping through on how to create and setup a SilverStripe Route. Never worked.

Avatar
mediabeastnz

Community Member, 10 Posts

4 December 2014 at 7:37pm

Wasn't a straight copy and paste but yeah what you posted is what I have and it doesn't work...

That's all i'm after just someone to explain how to do this correctly.

It's quite an important part of using SilverStripe as more than just a CMS e.g. Shopping cart, advanced management systems etc.

Any help is much appreciated ;)

Avatar
Fred Condo

Community Member, 29 Posts

6 December 2014 at 6:42am

Hi, mediabeastnz --

Whenever you add or edit configuration (private static variables or YAML configuration), you have to do a dev/build, either by going to /dev/build on the web site, or by doing "framework/sake dev/build" on the command line (cd to the docroot first). Then your route ought to be noticed.

Avatar
mediabeastnz

Community Member, 10 Posts

6 December 2014 at 10:53am

Hi Fred C, definitely tried a dev build, flush, flush=1, flush=all...you name it. :/

Avatar
Fred Condo

Community Member, 29 Posts

6 December 2014 at 12:40pm

Hi, I got it to work with this code, which follows the pattern from http://beta.doc.silverstripe.org/en/developer_guides/controllers/routing/#url-handlers :

<?php

class AccountPage extends Page {
	
}

class AccountPage_Controller extends Page_Controller {

	private static $allowed_actions = array(
		'delete_address'
	);
	private static $url_handlers = array(
		'address/$Action/$ID' => 'delete_address'
	);

	public function delete_address($r) {
		Debug::dump($r);
		return true;
	}

}

Avatar
mediabeastnz

Community Member, 10 Posts

18 December 2014 at 12:37pm

Using the example on the beta docs I did manage to get this to work.

Rather than having the url handlers in the controller I set them in the Yaml as per the docs and this seemed to work straight away.