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

Controller URL


Go to End


3 Posts   1440 Views

Avatar
alana

Community Member, 7 Posts

22 February 2014 at 12:04pm

Hi,

I am following the advice given in this thread:

http://www.silverstripe.org/general-questions/show/20711

"You can execute a controller function using the the $Page/$Action/$Parameter url pattern, where $Page is the URL, the $Action is the name of the controller function and $Parameter is an variable you may need to push."

Therefore I am attempting to call a controller function as follows:

http://127.0.0.1/silverstripe/index.php/homePage/test/

In my Page.php I have the following:

<?php
class Page extends SiteTree {

	private static $db = array(
	);

	private static $has_one = array(
	
	);
	
	public static $allowed_actions = array (
      'test'
    ); 
	
	public function test(){
		echo "hello world!";
	}

}

class Page_Controller extends ContentController {

	private static $allowed_actions = array (
	);

	public function init() {
		parent::init();

		Requirements::themedCSS('reset');
		Requirements::themedCSS('layout'); 
		Requirements::themedCSS('typography'); 
		Requirements::themedCSS('form'); 
	}

}

This is a fresh install of version 3.12, it couldn't be any more rudimentary - the output I get is "Page cannot be found".

Any suggestions?

Thanks,

Alan.

Avatar
Willr

Forum Moderator, 5523 Posts

22 February 2014 at 2:06pm

Couple points:

Does the page exist in the CMS? Say you have a home page created in the CMS with the URL 'Home' the route would be /home/action. If you don't want to have to create the page in the CMS then you have to define the full route in routes.yml

Your test function needs to be in the controller, not the model.

You need to make sure 'test' is listed as an allowed action on the controller.

http://doc.silverstripe.org/framework/en/topics/controller explains some of the concepts for general controllers. Page_Controllers have the slight difference in that a Page must exist in the CMS for them to work.

Avatar
alana

Community Member, 7 Posts

22 February 2014 at 11:48pm

That fixed it, it wasn't in the allowed actions, adding it gives me the expected output.

Many thanks!

Alan.