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.

General Questions /

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

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

Controller URL


Reply


3 Posts   442 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, 5513 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.