Robbo
Well-known member
Building a site using (around) XenForo
I need to create a proper site for MercenaryDesign and have decided while I do it I will write down what I do to help other people as I have seen a lot of people ask about this on these forums. This will be a basic set up, ready to expand, with a home page, different container template to make the style look different for the main site and setting a route for the forum index. This will also give people an in-site into some of the thinking that is behind making things for XenForo and how you have to think of each little part and design in your head before coding. Good programmers spend more time designing code than actually writing it.
Note: I will be using XenPlus for this. A developers library I have made. I will attach it to this post but use with care as it isn't in a release state yet. It will just be for listeners in this tutorial so you can just use the traditional way instead if you want.
Initial Setup
First we need to create a library and add-on. I recommend naming the library {abbr}Site incase you have more than one site running from the same files. I will be using MercSite and using that name throughout this tutorial. Navigate into /your/install/path/library/ and create a folder called MercSite. Then create a few folders we will need at some point later. ControllerPublic, Route, Route/Prefix, Listener.
Now go into ACP -> Development -> Create Add-on. Fill this out however you want, doesn't really matter unless you want to export your site at a later date (cool that you can do that, huh? <3 XenForo). Now we are ready to add our code and ACP changes.
Note: For the remainder of this tutorial we will use your/install/path/library/MercSite as the root directory.
Home Page
Now so we have something to look at later on when testing we will create the home page. First thing to do is make a route prefix. That is, to go to a certain class which will direct to the correct controller if there is a certain word in your URL. For this we will use home (later on we will set this route as the default one). This route prefix class will be called when you go to yourwebsite.com/home. Create a file called Home.php in Route/Prefix. File contents...
This is the most basic of route prefixes. This return line specifies 3 simple things:
- the controller we want to use (which we will create later)
- the route path for input processing later in your controller
- the navigation tab this section belongs to so it can be shown as selected. For example if you set this to 'help' then when at this route the help navigation tab would show as selected.
Now that we have our route class we can create the actual route in the ACP to use it. Go into ACP -> Development -> Route Prefixes -> Create new Route Prefix.
Set...
Ok if you navigate to yoursitename.com/home you should get this error: The controller MercSite_ControllerPublic_Home does not define an action called Index.
This is simply because we haven't created our Controller yet, let's do that now.
Create a file called Home.php in ControllerPublic. Give it the following contents.
Another simple little class. After routing XenForo will call the method here called actionIndex(). All we are doing with it is returning a View to display a page. First argument sent is to a View class which is optional and the second is the public template name, in this case I have just used 'home'.
Now that we have our Controller setup you should see that the home page you got the error on earlier is simply blank. To solve this we need to create the home template we specified.
Go into ACP -> Appearance -> Create New Template. Set the template name to home, add-on to MercSite and whatever HTML contents you want for your home page. Here is a simple little thing I added for testing.
Home page done!
I need to create a proper site for MercenaryDesign and have decided while I do it I will write down what I do to help other people as I have seen a lot of people ask about this on these forums. This will be a basic set up, ready to expand, with a home page, different container template to make the style look different for the main site and setting a route for the forum index. This will also give people an in-site into some of the thinking that is behind making things for XenForo and how you have to think of each little part and design in your head before coding. Good programmers spend more time designing code than actually writing it.
Note: I will be using XenPlus for this. A developers library I have made. I will attach it to this post but use with care as it isn't in a release state yet. It will just be for listeners in this tutorial so you can just use the traditional way instead if you want.
Initial Setup
First we need to create a library and add-on. I recommend naming the library {abbr}Site incase you have more than one site running from the same files. I will be using MercSite and using that name throughout this tutorial. Navigate into /your/install/path/library/ and create a folder called MercSite. Then create a few folders we will need at some point later. ControllerPublic, Route, Route/Prefix, Listener.
Now go into ACP -> Development -> Create Add-on. Fill this out however you want, doesn't really matter unless you want to export your site at a later date (cool that you can do that, huh? <3 XenForo). Now we are ready to add our code and ACP changes.
Note: For the remainder of this tutorial we will use your/install/path/library/MercSite as the root directory.
Home Page
Now so we have something to look at later on when testing we will create the home page. First thing to do is make a route prefix. That is, to go to a certain class which will direct to the correct controller if there is a certain word in your URL. For this we will use home (later on we will set this route as the default one). This route prefix class will be called when you go to yourwebsite.com/home. Create a file called Home.php in Route/Prefix. File contents...
Code:
<?php
class MercSite_Route_Prefix_Home implements XenForo_Route_Interface
{
public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
{
return $router->getRouteMatch('MercSite_ControllerPublic_Home', $routePath, 'home');
}
}
- the controller we want to use (which we will create later)
- the route path for input processing later in your controller
- the navigation tab this section belongs to so it can be shown as selected. For example if you set this to 'help' then when at this route the help navigation tab would show as selected.
Now that we have our route class we can create the actual route in the ACP to use it. Go into ACP -> Development -> Route Prefixes -> Create new Route Prefix.
Set...
Route Prefix: home
Route Type: Public
Route Class: MercSite_Route_Prefix_Home
Use class to build link: Never
Add-on: MercSite
Ok if you navigate to yoursitename.com/home you should get this error: The controller MercSite_ControllerPublic_Home does not define an action called Index.
This is simply because we haven't created our Controller yet, let's do that now.
Create a file called Home.php in ControllerPublic. Give it the following contents.
Code:
<?php
class MercSite_ControllerPublic_Home extends XenForo_ControllerPublic_Abstract
{
public function actionIndex()
{
return $this->responseView('MercSite_ViewPublic_Home', 'home');
}
}
Now that we have our Controller setup you should see that the home page you got the error on earlier is simply blank. To solve this we need to create the home template we specified.
Go into ACP -> Appearance -> Create New Template. Set the template name to home, add-on to MercSite and whatever HTML contents you want for your home page. Here is a simple little thing I added for testing.
Code:
<xen:title>Home</xen:title>
<div class="section">
<div class="primaryContent">Welcome to MercenaryDesign.</div>
</div>