1

I have created a small craft cms plugin to pull some information from the DB, and everytime I execute the plugin I get a 404.

Here is the controller

namespace Craft;

class UserExists_UserControllerController extends BaseController
{
    public function actionGetUser()
    {
        $this->returnJson($response);
    }
}

And this is the jquery:

$.ajax({
    url: "{{ url("actions/userexists/usercontroller/getUser") }}",
    dataType: "json",
    success: function(data){
        console.log(data)
    }
});
7
  • I've answered below, but wanted to offer one other (unrelated) recommendation: Your class's name can be shortened to UserExists_UserController—I suggest, only because the use or omission of the word "controller" in actions may become confusing! Using this class name will allow your AJAX call to be made to actions/userexists/user/getUser, instead. Commented Jul 31, 2017 at 21:49
  • Another possibility is to use a top-level controller—if your plugin isn't apt to have many other controllers or actions, you could simplify the naming even further: UserExists_Controller, and access it via actions/userexists/getUser. Commented Jul 31, 2017 at 21:53
  • I'm concerned, though, that the 404 error you're getting may have something to do with the naming of the file and class. Could you pots the filename that you've defined UserExists_UserControllerController in? Commented Jul 31, 2017 at 21:53
  • Hi! thanks so much for your reply. I have done that i keep getting 404
    – MariaZ
    Commented Jul 31, 2017 at 23:28
  • Do you mean UserExistsPlugin.php ?
    – MariaZ
    Commented Jul 31, 2017 at 23:32

3 Answers 3

4

If you aren't currently logged in, you're apt to be running up against the "anonymous access" limitation.

This section of the Controller documentation should help:

By default, controller actions are only accessible to logged-in users. You can override that by changing the protected $allowAnonymous property on your controller class. To allow anonymous access to all of your controller’s actions, set it to true:

<?php
namespace Craft;

class CocktailRecipes_IngredientsController extends BaseController
{
  protected $allowAnonymous = true;

  // Your action methods
}
1
  • Hi thank you... I tried that it doesnt work
    – MariaZ
    Commented Jul 31, 2017 at 23:30
2

Two things to try:

1) Try to use the Craft.getActionUrl() to get the URL of the action, as per the documentation for Linking Directly to Controller Actions:

2) Make sure there is no $this->requireLogin() or $this->requirePostRequest() in your controller

Theoretically there could be something wrong in the routing as well, as I have also experienced that a plugin runs perfectly, but because the response was not set to reply or re-route to somewhere meaningful, I got a 404 when accessing the controller via URL, and not from the control panel. In that case, this answered question could be helpful: Controller action works but still gives 404.

1
  • Thaank you!!... that links sounds interesting... I will have a look
    – MariaZ
    Commented Aug 2, 2017 at 1:18
1

If you are still not having any luck, If none of the above work, check out this thread that shows how to call an ajax function from a controller.

2
  • I have seen that thread I made it look the same and nothing... but thaaanks!
    – MariaZ
    Commented Aug 2, 2017 at 1:18
  • Try making your ajax request path start with a /. Example: /actions/pluginHandle/checkStatus
    – Damon
    Commented Aug 2, 2017 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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