XF 2.2 Best way to redirect group from resource_view to members_profile

Robert9

Well-known member
Best way to redirect someone from resource_view to members_profile

with returning a view?

Code:
        $resource = $this->assertViewableResource($params->resource_id, $this->getResourceViewExtraWith());

        $visitor = \XF::visitor();
        if ($visitor->user_id != 1)
        {
            $viewParams = [
                'user' => $resource->User
            ];
            return $this->view('XF:Members\Members', 'member_view', $viewParams);
        }

?
 
Last edited:
it will re-process all the steps in the member page load instead of just showing the template with what data you can stuff into it.

both may work, but i think the redirect is the more elegant and 'secure' approach.
 
And it saves the step from -> to array. :)
Thank you.

I have added it quick and dirty to the c/p/resourceitem/view

You may tell me, please:

I should not extend class and replace the whole funktion, right?

Code:
    public function actionView(ParameterBag $params)
    {
        $this->assertNotEmbeddedImageRequest();

        $resource = $this->assertViewableResource($params->resource_id, $this->getResourceViewExtraWith());

        // --- testing ---
        $visitor = \XF::visitor();
        if ($visitor->user_id != 1)
        {
            return $this->redirect($this->buildLink('members/', $resource->User));
        }
        // --- to do ---

....


Instead i extend and

Code:
    public function actionView(ParameterBag $params)
    {
      $parent=parent::actionView($params);

        $this->assertNotEmbeddedImageRequest();

        $resource = $this->assertViewableResource($params->resource_id, $this->getResourceViewExtraWith());

        // --- testing ---
        $visitor = \XF::visitor();
        if ($visitor->user_id != 1)
        {
            return $this->redirect($this->buildLink('members/', $resource->User));
        }
        // --- to do ---

      return $parent;
    }


right?
 
instead of extending, i would reroute the controller where you want it: (but, i don't know what is better)

Code:
 return $this->rerouteController(__CLASS__, 'actionIndex', $params);
 
What do you mean with reroute?
I dont want to change the resource manager, so i extend

<extension from_class="XFRM\Pub\Controller\ResourceItem" to_class="Robert9\UserResource\XFRM\Pub\Controller\ResourceItem" execute_order="10" active="1"/>

and

Code:
<?php

namespace Robert9\UserResource\XFRM\Pub\Controller;

use XF\Mvc\ParameterBag;
use XF\Mvc\Reply\AbstractReply;
use XF\Pub\Controller\AbstractController;

class ResourceItem extends XFCP_ResourceItem
{

   public function actionView(ParameterBag $params)
   {
        $parent=parent::actionView($params);

        $this->assertNotEmbeddedImageRequest();

        $resource = $this->assertViewableResource($params->resource_id, $this->getResourceViewExtraWith());

        $visitor = \XF::visitor();
        if ($visitor->user_id != 1)
        {
            return $this->redirect($this->buildLink('members/', $resource->User));
        }

        return $parent;
    }
}

Is this not ok?
 
Back
Top Bottom