XF 2.1 Renderer Files

Ozzy47

Well-known member
Working with XF/Mvc/Renderer/Rss.php I am looking to return a error on the page if someone accesses the RSS url directly. I can't for the life of me seem to get it worked out how to show a error instead of just a blank page. I can keep the 404 or not, but I would like a error to be shown.

PHP:
protected function initialize()
    {
        parent::initialize();
        $this->response->contentType('text/html');
        $this->response->httpCode(404);

        return $this->noPermission();
}
 
The only solution I can think of is to make use of controller_post_dispatch event and set the event class to controller name which return rss content. So something like this:
PHP:
    public static function controllerPostDispatch(\XF\Mvc\Controller $controller, $action, \XF\Mvc\ParameterBag $params, \XF\Mvc\Reply\AbstractReply &$reply)
    {
        if ($reply->getResponseType() === 'rss')
        {
            // more stuff goes in here

            $reply = $controller->noPermission(\XF::phrase('my_message_goes_here'));
        }
    }

Edit: No need to call controller plugin as controller already exposes noPermission().
 
Last edited:
The only solution I can think of is to make use of controller_post_dispatch event and set the event class to controller name which return rss content. So something like this:
PHP:
    public static function controllerPostDispatch(\XF\Mvc\Controller $controller, $action, \XF\Mvc\ParameterBag $params, \XF\Mvc\Reply\AbstractReply &$reply)
    {
        if ($reply->getResponseType() === 'rss')
        {
            // more stuff goes in here

            /** @var \XF\ControllerPLugin\Error $errorControllerPlugin */
            $errorControllerPlugin = $controller->plugin('XF:Error');
            $reply = $errorControllerPlugin->noPermission(\XF::phrase('my_message_goes_here'));
        }
    }

.or just)
PHP:
public static function controllerPostDispatch(\XF\Mvc\Controller $controller, $action, \XF\Mvc\ParameterBag $params, \XF\Mvc\Reply\AbstractReply &$reply)
    {
        if ($reply->getResponseType() === 'rss')
        {
            // more stuff goes in here

            $reply = $controller->noPermission(\XF::phrase('my_message_goes_here'));
        }
    }
 
The only solution I can think of is to make use of controller_post_dispatch event and set the event class to controller name which return rss content. So something like this:
PHP:
    public static function controllerPostDispatch(\XF\Mvc\Controller $controller, $action, \XF\Mvc\ParameterBag $params, \XF\Mvc\Reply\AbstractReply &$reply)
    {
        if ($reply->getResponseType() === 'rss')
        {
            // more stuff goes in here

            $reply = $controller->noPermission(\XF::phrase('my_message_goes_here'));
        }
    }

Edit: No need to call controller plugin as controller already exposes noPermission().
.or just)
PHP:
public static function controllerPostDispatch(\XF\Mvc\Controller $controller, $action, \XF\Mvc\ParameterBag $params, \XF\Mvc\Reply\AbstractReply &$reply)
    {
        if ($reply->getResponseType() === 'rss')
        {
            // more stuff goes in here

            $reply = $controller->noPermission(\XF::phrase('my_message_goes_here'));
        }
    }

Use that function in my XF/Mvc/Renderer/Rss.php file?
 
Try in Chrome/Chromedge or even Internet Explorer. Firefox does not support RSS or I haven't been able to find any flags for it.
 
Edge browser shows this:
<?xml version="1.0" encoding="utf-8"?> <errors> <error><![CDATA[The requested page cannot be represented in this format.]]></error> </errors>
 
This is what I get:
XML:
<?xml version="1.0" encoding="utf-8"?>
<errors>
  <error><![CDATA[You do not have permission to view this page or perform this action.]]></error>
</errors>
 
I'd advise against all approaches mentioned so far.

The current objective appears to be:
return a error on the page if someone accesses the RSS url directly
I assume this only pertains to the forum RSS in which case your code should only give an error if the forum URL is accessed.

Why not just extend the relevant controller action?

You would be able to switch the response type to HTML (otherwise the error will still render as XML, as above) and just return a typical controller error.
 
I assume this only pertains to the forum RSS in which case your code should only give an error if the forum URL is accessed.
Correct.

Why not just extend the relevant controller action?

You would be able to switch the response type to HTML (otherwise the error will still render as XML, as above) and just return a typical controller error.
How so?
 
I still can't seem to get anything put together that works right. :( I am probably overthinking this.
 
.if you need give "no permissions" error at the link forums/-/index.rss, you need to extend the class XF\Pub\Controller\Forum and it should have something like this in it
PHP:
<?php

namespace BS\WhereIsMyRss\XF\Pub\Controller;

class Forum extends XFCP_Forum
{
    // this code completely overwrites the parent function
    protected function getForumRss(\XF\Entity\Forum $forum = null)
    {
        $this->setResponseType('html');
        return $this->noPermission();
    }
}
 
.if you need give "no permissions" error at the link forums/-/index.rss, you need to extend the class XF\Pub\Controller\Forum and it should have something like this in it
PHP:
<?php

namespace BS\WhereIsMyRss\XF\Pub\Controller;

class Forum extends XFCP_Forum
{
    // this code completely overwrites the parent function
    protected function getForumRss(\XF\Entity\Forum $forum = null)
    {
        $this->setResponseType('html');
        return $this->noPermission();
    }
}

That works out perfectly, and shows me I was overthinking the process. Thank you for helping out.
 
Back
Top Bottom