2

I have an existing J3 component admin view which fires off a Javascript Ajax call to a Json controller and fetches back data which I then incorporate into that view. I'm trying to replicate how I've done that into a J4 component but I can't seem to get it working.

The controller is at: administrator/components/com_erycdirectory/src/Controller/InputDataController.json.php

The namespace for that controller is Eryc\Component\ErycDirectory\Administrator\Controller, the class name is InputDataController and it extends \JControllerLegacy

The function in this controller that I want to access remotely is called getInputsByDirectoryId() and it is currently set to echo out a json encoded array with a single hard-coded key/value pair in it until I can get it working. Then I'll update it to fetch a model to access the database with.

The Ajax call in the template is being sent to

'index.php?option=com_erycdirectory&task=inputdata.getinputsbydirectoryid&format=json'

The error that comes back is "Invalid controller class: inputdata". Any suggestions as to what I might be missing would be very much appreciated.

1
  • Thank you for taking the bold step of posting your first question on JSE. Please take our tour while you wait for support/feedback.
    – mickmackusa
    Jan 11 at 21:27

1 Answer 1

2

In J4 default MVC factory uses PSR-4 autoloading. So InputDataController.json.php filename is invalid, your controller class will not be found at all. Secondly, default component dispatcher doesn't use format for creating controllers anymore. The simple solution here is to rename the file to InputDataController.php.

If you want to customize the class structure, you can extend Joomla\CMS\MVC\Factory\MVCFactory and Joomla\CMS\Dispatcher\ComponentDispatcher or create your own implementations of Joomla\CMS\MVC\Factory\MVCFactoryInterface and Joomla\CMS\Dispatcher\DispatcherInterface.

3
  • Thanks Sharky, renaming the controller file was all it took. I've removed the format param from the URL as well and it's all working nicely.
    – Grant Hood
    Jan 12 at 9:08
  • You should keep the format param. This ensures that the correct document instance is created in the application and correct headers are sent. Although I'm guessing you do that already in the controller and manually close the application?
    – Sharky
    Jan 12 at 9:44
  • Ah, I misunderstood what you were getting at when you said format isn't used for creating controllers any more. I can reinstate it into the URL again, but can you expand on what you mean about ensuring the correct document instance is created and if I didn't have the format param, how should the application be closed?
    – Grant Hood
    Jan 12 at 10:34

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.