• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Controller, router and me.

dmnkhhn

Active member
#1
I have asked a similar question before but I still haven't found a good solution so far.

For my add-on I am currently using 2 PublicControllers, each with a separate route.

The url structure looks like this:

myxenforo.com/bookshelf (index, with all bookshelves)
myxenforo.com/bookshelf/1 (bookshelf, id1)
myxenforo.com/bookshelf/2 (bookshelf, id1)

myxenforo.com/book (index, with all books)
myxenforo.com/book/1 (book, id1)
myxenforo.com/book/2 (book, id1)


That's totally fine (for now) but I have one big problem: There is no way to display all bookshelves by user X.
This would require an url structure like myxenforo.com/bookshelf/member/1 (or similar)

With my current setup, it asks me for an action 'Member1' when want to open a link like this.

I am implementing the XenForo_Route_Interface, this is my method:
PHP:
    public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router) {
        $action = $router->resolveActionWithIntegerParam($routePath, $request, 'book_id');
        return $router->getRouteMatch('Doh_Books_ControllerPublic_Book', $action, 'book');

    }

What do I have to change to be able to use
myxenforo.com/bookshelf/member/1
or
myxenforo.com/book/member/1

?

Thanks for your help. :)
 

Mike

XenForo developer
Staff member
#2
The resolveActionWithIntegerParam method (and the converse version for link building) is really just a helper method. If you want to do something more complex (which this would fit under), you will have to do more low level structural parsing. (There are a few places we have done this, but usually it's just a pain.)

Though as an aside, what about members/1/bookshelf?
 

dmnkhhn

Active member
#3
Thanks Mike, I will take a closer look at the XenForo code. :)

members/1/bookshelf sounds good, too. Can this be done without breaking the default routing that handles the member stuff?
 

Mike

XenForo developer
Staff member
#4
Yeah, it's really setup for it already. You just need to use load_class_controller to extend the Member controller for your action.
 

dmnkhhn

Active member
#5
Thanks Mike, it's working like a charm. :)

Here's what I did:
I created a new code event listener that listens to 'load_class_controller'.

The execution callback is: Doh_Books_Listeners_LoadClassController::loadClassListener

My listener (library/Doh/Books/Listeners/LoadClassController.php) looks like this:
PHP:
<?php
class Doh_Books_Listeners_LoadClassController
{
    /**
     * @static
     * @param  $class
     * @param  $extend
     * @return void
     */
    public static function loadClassListener($class, &$extend)
    {
        if ($class == 'XenForo_ControllerPublic_Member')
        {
                $extend[] = 'Doh_Books_ControllerPublic_MemberBook';
        }
    }
}

As far as I can see, I need the separate controller for this because otherwise it is going to overwrite the Member controller with all actions from my main controller.