user6035846
user6035846

Reputation:

How to Properly Setup Silvertripe Routes and Controllers and Templates

How to Properly Setup Silvertripe Routes and Controllers and Pages? I followed the developers documentation as shown below,

TestController.php

    class TestController extends Controller {

        private static $allowed_actions = array(
            'test'
        );

        public function test(SS_HTTPRequest $request) {
            print_r('Executing Test Controller inside TestController');
        }

    }

routes.yml

---
Name: mysiteroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    'test/': 'TestController'

My url when entered is http://127.0.0.1/silverstripe/test/.

but the print_r from the test conroller did not appear also return $this->renderWith("Test") did not work. There is an actual Test.ss.

This is what the output was instead

Getting Started

To get started with the SilverStripe framework:

Create a Controller subclass (doc.silverstripe.org/framework/en/topics/controller)
Setup the routes.yml to your Controller (doc.silverstripe.org/framework/en/reference/director#routing).
Create a template for your Controller (doc.silverstripe.org/framework/en/reference/templates)

Community resources

silverstripe.org/forum Discussion forums for the development community.

silverstripe.org/irc IRC channel for realtime support and discussions.

doc.silverstripe.org Searchable developer documentation, how-tos, tutorials, and reference.

api.silverstripe.org API documentation for PHP classes, methods and properties.

Upvotes: 1

Views: 203

Answers (1)

Greg Smirnov
Greg Smirnov

Reputation: 1592

Director.rules configuration test: TestController binds any URL of the '/test/$Action' pattern to the TestController. Default action is called index.

So, if you want to handle just one action, then you don't need $allowed_actions, and rename test method to index.

Your current controller test method handles /test/test request.

Default template name is determined from the controller name with pattern TemplateName_Controller. The underscore character splits the name and is equivalent to calling $this->renderWith(['TemplateName','Controller']).

In your case default controller template is "TestController.ss" and custom action template may be "TestController_Test.ss" (as pattern {$DefaultTemplate}_{$Action}.ss)

You can debug your request with ?debug=1 and ?debug_request=1 query parameters.

Upvotes: 1

Related Questions