<p>I'm building a module, and all is going well. I can access all &quot;routes&quot; except <code>actionIndex</code>.</p>
<p>I have a controller named SendController.php</p>
<p>And, I can access the method below using &quot;actions/module-name/send/ajax&quot;</p>
<pre><code>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' =&gt; 'Round trip via AJAX!'];
        return $this-&gt;asJson($response);
    }
}
</code></pre>
<p>However, I can't access <code>actionIndex</code> using the same notation, &quot;actions/module-name/send&quot; or &quot;actions/module-name/send/index&quot;</p>
<p>I was able to register a route in the ModuleName.php file to access <code>actionIndex</code>, but I would like to understand why I can't access it in the same way as other &quot;actions&quot; in the controller.</p>
<p>Also, I was able to access via <code>{{ actionInput('module-name/send') }}</code> in a form, but I'm looking to build AJAX logic.</p>
<p>Is the &quot;index&quot; method protected for some reason, or am I missing something?</p>

CC BY-SA 4.0
2021-07-30T10:02:24.867Z
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.
Amit Senjaliya
CC BY-SA 4.0
2021-07-30T10:25:14.303Z
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
CC BY-SA 4.0
2021-07-31T03:14:59.977Z
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.
Tim Strawbridge
CC BY-SA 4.0
2021-07-31T14:14:20.170Z
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!
Dario Zadro
CC BY-SA 4.0
2021-08-02T14:16:05.220Z
@DarioZadro: I think what you're asking is why controllers need to ``return`` something instead of ``echo``?
RitterKnight
CC BY-SA 4.0
2021-08-02T15:30:03.920Z
@RitterKnight - Actually, my question was more about why /index wasn't accessible, but seems I've figured it out with `return` vs `echo`. And sure, if you can expand on that in an answer, that would be great.
Dario Zadro
<craft3><controller>
Help with understanding Controller routing
# Help with understanding Controller routing

(Asked by: username_0 on 2021-07-29)

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?

**Tags:** craft3, controller

> **Comments:**
> **username_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.  
> **username_2**: 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.  
> **username_3**: 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.  
> **username_0**: 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!  
> **username_4**: @DarioZadro: I think what you're asking is why controllers need to ``return`` something instead of ``echo``?  
> **username_0**: @username_4 - Actually, my question was more about why /index wasn't accessible, but seems I've figured it out with `return` vs `echo`. And sure, if you can expand on that in an answer, that would be great.  

---

## 0 answers

