XF 2.0 Guidance Please: Creating "Page" via Addon

LPH

Well-known member
Using an addon, I'm wanting to create a "page/section" called documentation.

I've followed the guide for building an addon to replace IndexAction for Members and the code works fine. Now, I'm trying to build a "Hello World" addon that would have the route http://192.168.1.152:8888/community/documentation/

This is what I have so far in a file named Documentation.php:

PHP:
namespace TRN\HelloWorld\XF\Pub\Controller;

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

class Documentation extends AbstractController
{
   public function actionIndex(ParameterBag $params)
   {
      return $this->message('Hello World');
   }
}

Next, I made a class extension for the base class XF\Pub\Controller\AbstractController and assigned it to the addon.

The next step is fuzzy to me. There needs to be a public router to the /documentation. I tried to add to Routes but wasn't sure.

Hello World Documentation Routes.webp

Going to the url ]http://192.168.1.152:8888/community/documentation fails to load the hello world.

The requested page could not be found. (Code: invalid_controller, controller: XF:Documentation, action: index)

The message is saying it is an invalid controller but I thought XF: Documentation was correct because I was following the index route.

Does anyone have any suggestions for the next step?
 
I tried to add the namespace to the router but this came up with the same error.

TRN\HelloWorld\XF\Pub\Controller:Documentation
 
If you're creating a new class with a new route, you don't need to create a class extension. You'd only need to do that if you were extending an existing class to add an action or something similar.

Your documentation class looks good but in lieu of the above, it doesn't need to be contained in an XF/ directory since you're not actually extending and adding actions to an existing controller. Move the documentation class to TRN/HelloWorld/Pub/Controller. Don't forget to update the namespace in your documentation class to account for the new path.

Finally, in your route, the controller should then be updated to TRN\HelloWorld:Documentation.
 
Or to phrase it another way, XF:Documentation would be the shortform for /src/XF/Pub/Controller/Documentation.
TRN\HelloWorld:Documentation would then map to /src/addons/TRN/HelloWorld/Pub/Controller/Documentation.
 
OK. Figured it out and then I see your reply @NixFifty and @S Thomas :)

The key is the difference between \XF\Pub\Controller and \Pub\Controller as well as what I was writing in the router.

Step 1: Documentation.php in the addons\TRN\HelloWorld\Pub\Controller
PHP:
namespace TRN\HelloWorld\Pub\Controller;

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

class Documentation extends AbstractController
{
   public function actionIndex(ParameterBag $params)
   {
      return $this->message('Hello Documentation');
   }
}

Step 2: No class extension.

Step 3: Documentation router:

Documentation Router.webp


Result:

Screen Shot 2018-07-17 at 11.45.27 AM.webp
 
X:Y is always a shortcut, where X represents your add-on ID and Y represents your class name. The colon is replaced depending on the context, so if you throw X:Y into a place that expects an entity, it's resolved to X\Entity\Y.php, for a public controller it's going to be X\Pub\Controller\Y.php. You can use as many subfolders as you deem necessary, so X:Y\Z\W is going to be resolved to X\Pub\Controller\Y\Z\W.php for a controller.

You only want to use the XF subfolder in your add-on when extending default XF classes through the class proxy system. So when extending the forum controller, you want to have that file in X\XF\Pub\Controller\Forum.php instead of X\Pub\Controller\Forum.php. It doesn't have to be, but it makes things more organized and we treat it as standard. The same goes for classes from other add-ons, where you basically just put the other add-ons ID as subfolder into your own. Extending a class from Y\Z would end up in X\Y\Z\WhatEverYouExtend. Classes like your controller that you newly create just directly go into the root folder of your add-on (in the relevant subfolder), so your custom controller would go into X\Pub\Controller\Whatever.php, cause that's what's going to be searched for by the name resolver when doing X:Whatever.
 
Top Bottom