• This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

[Library] Set your own Route/Controller as Homepage

Shadab

Well-known member
Just a quick addon/library I put together for shifting the forum listing to /forum/ and setting the homepage to be handled by your controller's index action.

These instructions are for the next release of XenForo (Beta 6), which will hopefully require no patch for this to work. Patches for Beta 5 will be posted shortly in the second post below.

Installation
  1. Download the attached zip file.
  2. Extract and upload the contents inside your /library/ directory.
  3. There's no step 3. :)

Usage/API

There's only one static method which you need to call. The first argument (type: Zend_Config) is the configuration data you need to pass; and the second argument (type: array) is the dependency data supplied by init_dependencies.

GeekPoint_CustomIndex_Helper::setDefaultRoute($config, $data);

Configuration

routePrefix

This is the route prefix which you want to set as the default. Suppose you set the "foo" route-prefix as default, any links generated for "foo" or "foo/index" will now point to the homepage "/".​

controllerClass

This is the controller class which will handle the homepage. Specifically, the actionIndex() method in your controller will be responsible for generating the homepage.​

majorSection

[Optional] The major section-name, used when generating the route-match for homepage.​

minorSection

[Optional] The minor section-name, used when generating the route-match for homepage.​

params

[Optional] Parameters that will be added to the request object during the routing process. This enables you to set any content which depends upon a request parameter to be present, as your homepage. For example: node "Pages", or Threads (yes, threads! But for heaven's sake don't do that.)

Examples

Create a new event-listener for init_dependencies. Then in your callback method, just set the configuration options, and call the helper. That's it. Some examples are given below, starting with the code for setting "Recent Activity" as your homepage...

PHP:
public static function initDependencies(XenForo_Dependencies_Abstract $dependencies, array $data)
{
	if (!$dependencies instanceof XenForo_Dependencies_Public)
	{
		return;
	}

	$config = new Zend_Config(array(
		'routePrefix'     => 'recent-activity',
		'controllerClass' => 'XenForo_ControllerPublic_RecentActivity',
	));

	GeekPoint_CustomIndex_Helper::setDefaultRoute($config, $data);
}


@Jaxel:
Configuration for setting the portal page as homepage...

(No other changes required.)

PHP:
	$config = new Zend_Config(array(
		'routePrefix'     => 'portal',
		'controllerClass' => 'EWRporta_ControllerPublic_Portal',
		'majorSection'    => 'portal'
	));


@Brogan:
Configuration for setting a node page as the homepage.

(Replace "lectus-pretium-consequat" with your node name. Any changes that you make to the page name via Admin Panel need to be reflected in this config array.)

PHP:
	$config = new Zend_Config(array(
		'routePrefix'     => 'pages',
		'controllerClass' => 'XenForo_ControllerPublic_Page',
		'params'          => array('node_name' => 'lectus-pretium-consequat'),
	));


License

Since this addon(?) is released under the MIT License, you are free to include and use this in your own addons & products. It would be nice if you leave the directory structure & the files included therein, unmodified; but it's not a requirement.

The complete license text is available in the LICENSE file inside the release package.

Related Threads
 

Attachments

Patches for XenForo Beta 5 (only):

(1) /library/XenForo/Link.php

Find: (line 377)
PHP:
		$handler = new $class();
Replace with:
PHP:
		$class = XenForo_Application::resolveDynamicClass($class, 'route_prefix');
		$handler = new $class();


(2) /library/XenForo/Link.php

Find: (line 272-275)
PHP:
		if ($prefix === 'index' && $action === '' && $extension === '')
		{
			return '';
		}
Remove it.
 
Random question. If someone were to-do this, how is the forum index made available?
 
The forum index gets moved to "/forum/".
Existing links to the forum index, eg. {xen:link index} will also point to this new url.
 
Great work Shadab!

Just to be clear, if my existing URL is http://xenforo.com/community/forums/announcements/
After installing this it will be http://xenforo.com/community/forum/announcements/

So /forums will change to /forum, is that correct?

And if I currently have a page like so: http://xenforo.com/community/pages/home/
I can now set it to be http://xenforo.com/home/

Or have I misunderstood?
 
No... it doesn't do that.

The reason why it has /forum/ vs /forums/ is because /forums/ already is it's own route prefix. Rather than interfering with that route prefix and forcing tons upon tons of more code which could bog things down, he simply created a new route for the index mapped to /forum/.

Either way, I think this feature should be built into XenForo... it should not be a separate mod.
 
I'm not sure I agree with how this works.

The URL structure changes from the forum home to individual forums.
Forums and threads are part of the /forum route so that shouldn't be removed.

That would be similar to clicking on Members and then an individual member and the /members portion of the URL being removed.
 
I'm not sure I agree with how this works.

The URL structure changes from the forum home to individual forums.
Forums and threads are part of the /forum route so that shouldn't be removed.

That would be similar to clicking on Members and then an individual member and the /members portion of the URL being removed.
I just clicked on basically a vanilla install, installed in my root (without this mod), and the link was: localhost/xen/index.php?forums/forum-name.1/. There was no forum/forums/. It happens here because XenForo is installed under a sub-directory. Without rewrites it'd be community/index.php?forums/forum-name.1/.

What this does is redefine the route prefix: index.php?index (or nothing) to be index.php?forum/ because it redefines the action of the index route to be the router & action that you set it to be.
 
Quick Comparison of route prefixes, used for matching:

Stock XenForo:
  • homepage: /index/ (points to the forum home)
  • individual forums: /forums/
  • individual threads: /threads/
  • individual pages: /pages/
Shifting forum home to its separate route-prefix:
  • homepage: /index/ (points to your own controller)
  • forum home: /forum/
  • individual forums: /forums/
  • individual threads: /threads/
  • individual pages: /pages/
Jaxel is right in that it's more work for me to reuse the /forums/ route prefix, because it already is used for displaying individual forums and sub-forums. So shifting the forum home to a separate route prefix (/forum/) is easier.
 
Thanks for the comparison Shadab.

Is the forum home: /forum/ in the second list related to the install path?
So here for example it would still be /community/ using your mod?
 
All the given paths/prefixes are relative to your installation directory.
Nothing gets shifted outside your install path.
 
Many thanks for the clarification.

I'll be installing this later on my local install to test it out.

Can't wait to migrate now :D
 
What it we have a wordpres blog lets say root/blog directory. How do we set up it as home page ? I was looking forward to this and give it a try.
 
Top Bottom