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

How do we define action for custom URL built using input?

TheBigK

Well-known member
#1
So far, in my test addon, I'm asking the user to input a string and then build a URL using that string. However, the new URL, where I'm redirected to says:

The controller Events_ControllerPublic_Event does not define an action called <my-entered-text.1>

I'd like to show the information entered on the page prior to redirect on this new page. What should I do?
 

batpool52!

Well-known member
#2
https://www.mydomain.com/controller-name/title_from_data.numeric_id_from_data/delete

controller-name is Events_ControllerPublic_Event
title_from_data and numeric_id_from_data is taken from the array that is passed to {xen:link xyz} or XenForo_link::buildPublicLink / XenForo_Link::buildAdminLink. For example {xen:link threads, $thread} or XenForo_Link::buildPublicLink('threads', $thread); and $thread contains a row from xf_thread table. /delete is the action name. Look at the route file to know which array key is used as title and which is used as numeric identifier.
 

TheBigK

Well-known member
#3
@batpool52! - Thank you for your response. I think I have not explained the question properly. Let me add my screenshots.

Here's what I'm displaying on localhost/xf/events/

Screen Shot 2015-09-10 at 7.26.42 pm.png

My actionAdd() saves all this data properly to the database using datawriter.

PHP:
<?php

class Events_ControllerPublic_Event extends XenForo_ControllerPublic_Abstract
{
    public function actionIndex()
    {
        //This will build the Add New Event Page on /events/add/
        $viewParams = array();
        return $this->responseView('Events_ViewPublic_Event', 'events_add_new', $viewParams);
    }

    public function actionAdd()
    {
        //Get information from user input
        $eventTitle = $this->_input->filterSingle('event_title', XenForo_Input::STRING);
        $event_info = $this->_input->filterSingle('event_info', XenForo_Input::STRING);

        //Create DataWriter
        $dw = XenForo_DataWriter::create('Events_DataWriter_Index');
        $dw->set('event_title', $eventTitle);
        $dw->set('event_info', $event_info);
        $dw->save();

        $data = $dw->getMergedData();

        return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, XenForo_Link::buildPublicLink('events', $data));

    }

}
So I can successfully build a link : http://localhost/xf/events/my-event-name.2/ , but this new link displays error:-

The controller Events_ControllerPublic_Event does not define an action called MyEventName.2.

My aim is to display the information entered in the 'Event Information' box from earlier page on this new page. Similar to what we do while creating a thread.
 

Liam W

Well-known member
#4
@batpool52! - Thank you for your response. I think I have not explained the question properly. Let me add my screenshots.

Here's what I'm displaying on localhost/xf/events/

View attachment 116552

My actionAdd() saves all this data properly to the database using datawriter.

PHP:
<?php

class Events_ControllerPublic_Event extends XenForo_ControllerPublic_Abstract
{
    public function actionIndex()
    {
        //This will build the Add New Event Page on /events/add/
        $viewParams = array();
        return $this->responseView('Events_ViewPublic_Event', 'events_add_new', $viewParams);
    }

    public function actionAdd()
    {
        //Get information from user input
        $eventTitle = $this->_input->filterSingle('event_title', XenForo_Input::STRING);
        $event_info = $this->_input->filterSingle('event_info', XenForo_Input::STRING);

        //Create DataWriter
        $dw = XenForo_DataWriter::create('Events_DataWriter_Index');
        $dw->set('event_title', $eventTitle);
        $dw->set('event_info', $event_info);
        $dw->save();

        $data = $dw->getMergedData();

        return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, XenForo_Link::buildPublicLink('events', $data));

    }

}
So I can successfully build a link : http://localhost/xf/events/my-event-name.2/ , but this new link displays error:-

The controller Events_ControllerPublic_Event does not define an action called MyEventName.2.

My aim is to display the information entered in the 'Event Information' box from earlier page on this new page. Similar to what we do while creating a thread.
You need to get the action using the resolveActionWithIntegerParam method.

The default action is index.
 

batpool52!

Well-known member
#5
This is the route code you would need to use
PHP:
<?php

class Events_Route_Prefix_Events implements XenForo_Route_Interface
{
    public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
    {
        $action = $router->resolveActionWithIntegerParam($routePath, $request, 'event_id');
        $action = $router->resolveActionAsPageNumber($action, $request);
        return $router->getRouteMatch('Events_ControllerPublic_Event', $action, 'forums'); // change forums to the navigation id
    }

    public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
    {
        $action = XenForo_Link::getPageNumberAsAction($action, $extraParams);

        return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'event_id', 'event_title');
    }
}
 

TheBigK

Well-known member
#6
Thanks @Liam W and @batpool52! . I really appreciate your responses.

I'm using the following code-

PHP:
<?php

class Events_Route_Public_Event implements XenForo_Route_Interface
{
    public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
    {
        return $router->getRouteMatch('Events_ControllerPublic_Event', $routePath);
    }


    public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
    {
        return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'event_id', 'event_title');
    }
}
looks like I got to do something with the $action in my buildLink method. o_O