• Welcome to Tux Reports: Where Penguins Fly. We hope you find the topics varied, interesting, and worthy of your time. Please become a member and join in the discussions.

Building A Local Testing Site for XenForo

This entry is part of a series of entries "Reading XenForo Code, A Place to Start"
Information in this post is packed with PHP terms. Ask in the comments if you are unclear about a term or something that is written.

Sometimes I stick a var_dump into a section of the XenWord code, click on a local test site, and look for the returned values. There is an easier way to handle testing without messing with a project and I want to show you.

Purpose

Let me walk you through a fast way to look at variables, methods, and classes, as well as how to learn more about XenForo. By the end of this post you will have the power to read XenForo code beyond simply opening up a PHP file and reading it. You will have a means to see the code in action through var_dump. You'll also end up with tons of questions. And that is called opportunity.

This entry is not about setting up a server with an installation of XenForo. XenForo already has these directions set for you. There is a nice guide on XenForo for XAMPP, too. This DevLog is about setting up a site to use for testing code and introducing a way to use the site to learn about XenForo.

Materials
The following items should be installed prior to following this series: MAMP, XenForo, and your favorite editor. I use PhpStorm and Atom. If you do not have these installed then please stop and install these packages (See Note 1).

Procedure


Part I: Create a New Site in MAMP
Create a new Apache based site called phptests in MAMP .
  • Create a folder on your hard drive and name it 'phptests.'
  • Open MAMP
  • Click on the + sign to create a new site
  • Fill in the name of the site
  • Browse to the proper folder
  • Save and restart the server
  • Open your browser and enter the name of the test site 'phptests/'
PHP Tests MAMP Creation.png


Modify the MAMP created index.php file

Once you know your site is good then open the index.php file and remove the contents. I prefer to insert in a little note.

PHP:
<?php
    $message = 'This site is for testing PHP';
    echo '<div align=center>' . $message . '</div>';

    // Do not erase or modify this file.

Part II: Create a file for connecting to XenForo

Rather than simply opening up a PHP file within the XenForo code base and reading it, we are going to connect our test files so that we can work with XenForo in an external environment.

Create a new PHP file called connectXenForo.php (See Note 2)
Copy the following PHP code into the connectXenForo.php file.

PHP:
<?php
/**
* Connects to XenForo
*/
if ( !class_exists( 'XenForo_Autoloader' ) ) {

  $startTime = microtime( true );

  /** @var  $fileDir */
  $fileDir = '/Users/layneheiny/Documents/Sites/lph/community';

  require_once( $fileDir . '/library/XenForo/Autoloader.php' );
  XenForo_Autoloader::getInstance()->setupAutoloader( $fileDir . '/library' );

  XenForo_Application::initialize( $fileDir . '/library', $fileDir, true, array( 'resetOutputBuffering' => false ) );
  XenForo_Application::set('page_start_time', $startTime );
  XenForo_Application::disablePhpErrorHandler();
}

Change Line 10, containing the $fileDir variable, to point to your local installation of XenForo. Use XenForo's phpinfo() included in the admin section to help you find the absolute path. Look in the xenforoInstall/admin.php?tools/phpinfo.

The second entry in this series will go over the contents of the connector file. For now, we want to learn how to use the connection of our site to XenForo.

Part III: Using the Connection to XenForo

Example 1: Node.php

Now that you can easily connect to XenForo, we can create test files to isolate methods and variables. We will require the file in other files.

Let's run through some examples of using the new test site. Let's say you want to learn about node_id_type. Easy.

Create a file called forumTests.php and enter the following code. When you point to this file in your browser then information about the node will be returned. A node_id of 1 is used since this is a common Category in a default installation of XenForo.

PHP:
<?php

// This line adds the information in the connectXenForo file
require_once 'connectXenForo.php';

$node_id = 1; // Change to any node you'd like

// Insert check if $node_id is for a forum node or a category
/** @var  $forumModel XenForo_Model_Node */
$forumModel = XenForo_Model_Node::create( 'XenForo_Model_Node' );

$forum = $forumModel->getNodeById($node_id);

$node_type = $forum['node_type_id'];

if ( $node_type == 'Category' )
{
  echo 'This is a category';
} else
{
  echo 'This is not a category';
}

Let's pick this code apart for a moment so you can understand a little more about XenForo as well as practice writing your own test files for variables and methods.

Line 1

1. <?php
Line 1 is the opening PHP tag. There is no closing tag on a PHP file. If you use PHP inline with HTML then you must close the PHP tag. For your own projects, you'll probably adopt the Model View Controller (MVC) approach, and never mix HTML and PHP.

Sample inline use of PHP; mixing of HTML and PHP.
PHP:
<h2>
<?php echo 'Hello World.'; >?
</h2>

Line 3

3. // This line adds the information in the connectXenForo file
Lines beginning with two slashes are single lines of comments. Many XenForo developers will tell you that they learned PHP and how to code XenForo addons by reading the comments within the files. In my view, this is an important step. It's also important to use the test environment to try to learn what is inside the XenForo objects.

Line 4

4. require_once 'connectXenForo.php';
Line 4 is the require_once line. This line gets the contents of the connectXenForo.php and adds it as if you had typed the contents. The name of the file is pretty obvious and this is important in programming with PHP. Don't use abbreviations. Spell file names and methods all out.

Line 6
6. $node_id = 1; // Change to any node you'd like
Line 6 is assigning the variable $node_id to a value of 1. As you play around with the code then you can change the 1 to different values. Equal signs were perplexing to me for a bit. A single equal sign is an assignment of a value to a variable. Two equal signs is used to check if the conditions hold true.

PHP:
$variable = 1; //Assignment of the value 1 to the variable

// Test the variable
if ( $variable == 1 )
{
     echo 'The variable is 1';
} else
{
     echo 'The variable is not 1';
}

Line 10
10. $forumModel = XenForo_Model_Node::create( 'XenForo_Model_Node' );
Line 10, the $forumModel line, is really cool. Here you create a new Node Model from XenForo. Object Oriented programming uses a Scope Resolution Operator (the two colons, :: ). In the single quotes is the model being created. You can find that model browsing to your XenForo installation >> library >> XenForo >> Model.

The underscore between the words XenForo, Model, and Node is equivalent to the path of the directories and ultimately the file name. Everything is relative to the /library directory within the first XenForo directory.

It's very important that you start opening up the model files in XenForo so that you can learn what is happening. Read their code so you can learn from the experts.

Line 12
12. $forum = $forumModel->getNodeById($node_id);
A symbol you see in line 12 is the dash with the greater than symbol. This "arrow" is the object operator. StackOverflow has a nice page explaining all the PHP symbols. Use the arrow to call a method with an object created. This means line 12 calls the getNodeById found in the Node.php file.

Open the Node.php file. The getNodeById method should be around line 90.

Line 14

14. $node_type = $forum['node_type_id'];
Now that we've assigned a $forum variable in line 12, we want to only grab a portion of the contents. We do this with the square brackets. This is the node_id_type, whether the node is a forum or category.

Using var_dump

OK. If you've followed along then let's have some fun.

One of my favorite learning tools is var_dump. Imagine you looked at line 12 and wasn't sure of the contents being returned by the getNodeById.

Insert the following below line 12.

PHP:
var_dump($forum);

Now read what is returned. You should see something similar to the following code in your browser (See Note 3).

Code:
/Users/layneheiny/Documents/Sites/phptests/forumTests.php:14:
array (size=14)
  'node_id' => int 1
  'title' => string 'Main Category' (length=13)
  'description' => string '' (length=0)
  'node_name' => null
  'node_type_id' => string 'Category' (length=8)
  'parent_node_id' => int 0
  'display_order' => int 1
  'display_in_list' => int 1
  'lft' => int 1
  'rgt' => int 6
  'depth' => int 0
  'style_id' => int 0
  'effective_style_id' => int 0
  'breadcrumb_data' => string 'a:0:{}' (length=6)

This is a category

The information returned shows that the $forum variable is an array. An array is a list of information. In line 7 of the list is the 'node_type_id' and it has a value of 'Category' - rather than 'Forum' - or another value.

Do you want to try more? Great !

Erase the var_dump line you inserted and let's insert another var_dump below Line 14. Save the file and open it in a browser.

PHP:
var_dump($node_type);

Code:
/Users/layneheiny/Documents/Sites/phptests/forumTests.php:16:string 'Category' (length=8)

This is a category

Oh my. Look at what was hiding in that array and you now know how to grab it. It's all about the square brackets.

Example 2: User Model

Let's create a file named userModel.php and put in the following code:

PHP:
<?php

require_once 'connectXenForo.php';

$user_id = 1;

$userModel = XenForo_Model::create('XenForo_Model_User');
$user = $userModel->getFullUserById($user_id);

var_dump($user);

This will return the following information about the array:

Code:
array (size=71)
  'user_id' => int 1
  'username' => string 'LPH' (length=3)
  'email' => string 'layne.heiny@gmail.com' (length=21)
  'gender' => string '' (length=0)
  'custom_title' => string '' (length=0)
  'language_id' => int 1
  'style_id' => int 0
  'timezone' => string 'America/Los_Angeles' (length=19)
  'visible' => int 1
  'activity_visible' => int 1
  'user_group_id' => int 2
  'secondary_group_ids' => string '3,4' (length=3)
  'display_style_group_id' => int 3
  'permission_combination_id' => int 5
  'message_count' => int 111
  'conversations_unread' => int 0
  'register_date' => int 1476595704
  'last_activity' => int 1480108740
  'trophy_points' => int 16
  'alerts_unread' => int 0
  'avatar_date' => int 0
  'avatar_width' => int 0
  'avatar_height' => int 0
  'gravatar' => string 'layne.heiny@gmail.com' (length=21)
  'user_state' => string 'valid' (length=5)
  'is_moderator' => int 1
  'is_admin' => int 1
  'is_banned' => int 0
  'like_count' => int 0
  'warning_points' => int 0
  'is_staff' => int 1
  'dob_day' => int 0
  'dob_month' => int 0
  'dob_year' => int 0
  'status' => string '' (length=0)
  'status_date' => int 0
  'status_profile_post_id' => int 0
  'signature' => string 'Signature line for LPH.' (length=23)
  'homepage' => string '' (length=0)
  'location' => string '' (length=0)
  'occupation' => string '' (length=0)
  'following' => string '' (length=0)
  'ignored' => string '' (length=0)
  'csrf_token' => string 'PD6c6NagwJ0tSc4O3trDMdRVB6lFWGxmyOigv4uj' (length=40)
  'avatar_crop_x' => int 0
  'avatar_crop_y' => int 0
  'about' => string 'BBCode
[url=http://www.tuxreports.com]Tux Reports[/url]' (length=55)
  'custom_fields' => string 'a:6:{s:3:"aim";s:0:"";s:8:"facebook";s:0:"";s:3:"icq";s:0:"";s:5:"skype";s:0:"";s:7:"twitter";s:0:"";s:5:"yahoo";s:0:"";}' (length=121)
  'external_auth' => string '' (length=0)
  'password_date' => int 1476595704
  'show_dob_year' => int 1
  'show_dob_date' => int 1
  'content_show_signature' => int 1
  'receive_admin_email' => int 1
  'email_on_conversation' => int 1
  'is_discouraged' => int 0
  'default_watch_state' => string '' (length=0)
  'alert_optout' => string '' (length=0)
  'enable_rte' => int 1
  'enable_flash_uploader' => int 1
  'use_tfa' => int 1
  'ubs_unread_blog_entries_count' => int 1
  'allow_view_profile' => string 'everyone' (length=8)
  'allow_post_profile' => string 'members' (length=7)
  'allow_send_personal_conversation' => string 'members' (length=7)
  'allow_view_identities' => string 'everyone' (length=8)
  'allow_receive_news_feed' => string 'everyone' (length=8)

Conclusion

If you are anything like me then you are exhausted at this point. You have tons of questions. You might even want to quit. Don't give up. You now have a test site, some test files, and a way to include XenForo so that you can learn more about the software.

The key point to remember is that you now have a way of working with the XenForo code without having to rely on others. From now on, you can look for yourself and try out code without buggering up another project.

Your Assignment
Create a new file that will do a var_dump of a variable using the method getAllNodes(). Start by looking around line 176 of the /library/XenForo/Model/Node.php file.

Notes
1. This post is based on an Apple environment. XAMPP is a Windows-based machine equivalent product.
2. This post assumes that you have XenForo installed already in another folder. If this is not the case then you must install XenForo first.
3. Xdebug must be enabled. MAMP Pro 4.x provides an easy way to enable Xdebug. It's a matter of a checkbox on the PHP Languages tab. The option is in the middle of the page, below the caching dropbox.
Next entry in series The Index File

Blog entry information

Author
LPH
Views
3,079
Last update

More entries in Technology

More entries from LPH

Top