0

I'm trying to create a controller within my custom module, but the controller still doesn't response when visiting the associated path.

Here are my files:

my_module/my_module.routing.yml:

my_module.agreement:
  path: 'my_module/send_consent'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyModuleController::check_agreement'
    _title: 'Agreement controller'
  methods:  [GET]

my_module/src/Controller/MyModuleController.php :

namespace Drupal\my_module\Controller;


use Drupal\Core\Controller\ControllerBase;

class MyModuleController extends ControllerBase {

    public function check_agreement() {
        die('hello from controller');
    }
}

To test I visit myproject.local/my_module/send_consent

Any help?

  • 1
    The question on this answer is not a duplicate, but the answer is relevant: drupal.stackexchange.com/a/224668/42650. Routes should begin with a slash. – mradcliffe Aug 10 '18 at 11:38
  • In this case my controller serve Ajax there is no problem if slash was not figure in path field of routing YAML file while the Ajax request is sent to "my_domain/my_module/send_consent" that slash surely will be always present in the url. – MiharbKH Aug 10 '18 at 15:18
2

You forgot requirements (which is required):

my_module.agreement:
  path: 'my_module/send_consent'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyModuleController::check_agreement'
    _title: 'Agreement controller'
  methods:  [GET]
  requirements:
    _permission: 'access content'

See Structure of roots for full details.

  • I thought it was optional! should we set permissions always to 'access content'?? – MiharbKH Jul 26 '18 at 11:01
  • 1
    @BrahimKhouy You can click the tick to accept the answer – Andy Jul 26 '18 at 13:35
  • 1
    The permission is whatever you need to require to access that path. – Kevin Jul 26 '18 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.