1

I'm building a module, and all is going well. I can access all "routes" except actionIndex.

I have a controller named SendController.php

And, I can access the method below using "actions/module-name/send/ajax"

class SendController extends Controller
{
    protected $allowAnonymous = ['index', 'ajax'];

    public function actionIndex()
    {
        return 'hello world'; //this works!
        echo 'hello world'; //does not work, throws a template error
    }

    public function actionAjax()
    {
        $response = ['response' => 'Round trip via AJAX!'];
        return $this->asJson($response);
    }
}

However, I can't access actionIndex using the same notation, "actions/module-name/send" or "actions/module-name/send/index"

I was able to register a route in the ModuleName.php file to access actionIndex, but I would like to understand why I can't access it in the same way as other "actions" in the controller.

Also, I was able to access via {{ actionInput('module-name/send') }} in a form, but I'm looking to build AJAX logic.

Is the "index" method protected for some reason, or am I missing something?

6
  • 1
    Heyy, the first index method is also accessible via ajax. when you used the index method to your code which error occurred?? Also how do you run ajax code please show with question. Jul 30 at 10:02
  • 1
    Common controller gotchas: check if the $allowAnonymous property includes your action (or is just set to true). Also if you POST to your action you need to send the proper CSRF token parameters.
    – gioppe
    Jul 30 at 10:25
  • 1
    In your modules controller file make sure "index" is in your protected $allowAnonomous array or just set it to true (I really wouldn't do the later though) -> protected $allowAnonymous = ['index', 'some-other-stuff'];. Also, if you are getting an error, let us know the error. Jul 31 at 3:14
  • Yes, I have $allowAnonymous set and found that the function didn't like echo only return worked. I've updated my question. Feel free to provide an answer for me to accept. Thanks, everyone! Jul 31 at 14:14
  • @DarioZadro: I think what you're asking is why controllers need to return something instead of echo? Aug 2 at 14:16

Your Answer

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

Browse other questions tagged or ask your own question.