Jump to:

23457 Posts in 18905 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Controller URL

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

Page: 1
Go to End
Author Topic: 314 Views
  • alana
    Avatar
    Community Member
    7 Posts

    Controller URL Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Controller URL Link to this post

    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.

  • alana
    Avatar
    Community Member
    7 Posts

    Re: Controller URL Link to this post

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

    Many thanks!

    Alan.

    314 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.