简体   繁体   English

Joomla:请求子控制器响应时找不到错误500视图

[英]Joomla: Error 500 View not found when requesting subcontroller response

I'm trying to return the result of a task from a subcontroller with the following url: 我正在尝试使用以下网址从子控制器返回任务的结果:

index.php?option=com_example&task=subctrl.test&format=json

but I keep getting the 500 View not found Error... 但我不断得到500 View not found错误...

class ExampleControllersSubctrl extends JControllerForm
{
    public function test()
    {
                $result= array("val1","val2");
        echo json_encode($result);
    }
}

I've tried naming the subcontroller file both Subctrl.php & Subctrl.json.php but neither worked. 我尝试同时命名Subcontroller文件Subctrl.php和Subctrl.json.php,但都没有用。 I believe I shouldn't need a view to render the result based on other SO posts I've read but maybe that is incorrect. 我相信我不需要基于我已阅读的其他SO帖子来呈现结果的视图,但这也许是不正确的。

This setup will eventually be used to return an Ajax call when I get it working. 当我开始工作时,此设置最终将用于返回Ajax调用。 What am I doing wrong here? 我在这里做错了什么?

Add an exit statement after the echo statement or Joomla will continue processing the component and will try to call a view. 在echo语句之后添加exit语句,否则Joomla将继续处理该组件并尝试调用视图。 Since no view value was set, no view will be found and the system will redirect to an error page. 由于未设置任何视图值,因此将找不到任何视图,并且系统将重定向到错误页面。 Full code below: 完整代码如下:

class ExampleControllerSubctrl extends JControllerForm
{
    public function test()
    {
        $result= array("val1","val2");
        echo json_encode($result);
        exit();
    }
}

Joomla also some other methods that you can use such as call jexit() or JFactory::getApplication()->close() . Joomla还可以使用其他一些方法,例如调用jexit()JFactory::getApplication()->close() The general idea is to get the application to stop here. 一般的想法是让应用程序在这里停止。 Continuing is a waste. 继续是浪费。

Also, had to make sure the class name is set right. 另外,必须确保正确设置类名。 Middle work should be Controller not Controllers . 中间工作应该是Controller而不是Controllers

The problem is that you're extending JControllerForm which will try and guess the view for your form if one isn't provided. 问题是您正在扩展JControllerForm ,它将尝试猜测formview (如果未提供)。

On Joomla 2.5 you can change JControllerForm to JController and that will resolve the problem. 在Joomla 2.5上,您可以将JControllerForm更改为JController ,这将解决问题。

As you have a JSON controller that Joomla is routing you to via format=json you don't need an exit on your test() method either. 由于您有Joomla通过format=json您路由到的JSON控制器,因此您也不需要在test()方法上退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM