|Classes of Barton Phillips||>||PHP Site Class||>||docs/examples.md||>||Download|
The code shown below can be found in the 'examples' directory at http://github.com/bartonlp/site-class or from your project root at 'vendor/bartonlp/site-class/examples'. There is an 'EXAMPLES.md' and EXAMPLES.html in the 'examples' directory.
<p style="color: green">The code in the 'examples' directory has actually been tested and runs. The code in this README was originally copied in from the examples code but may have changed for some reason. Therefore you should use the examples code rather than doing a copy and past from this README.</p>
If you have Apache or Nginx installed then you should have made your project root somewhere within your DocumentRoot, for example '/var/www/html/myproject'.
If you don't have Apache or Nginx installed on your computer you can use the PHP server. Do the following from your project root:
Then use your browser by entering
The code in the 'examples' directory uses the sqlite3 database engine. There should be a 'test.sdb' database file in the 'examples' directory already.
I have included a 'sqlite.sql' file that can be run from the command line if you want to recreate the 'members' table.
You will need to get sqlite3 and get the PHP sqlite packages along with mysql etc. From the command line in the directory where the SiteClass was downloaded:
This should create a new 'members' table in the 'test.sdb' database.
There are a number of ways to use the framework:
First you can just use the SiteClass all by itself.
That is the simplest usage. You get a generic head and a generic footer. No database or other stuff.
You can extend this by adding a database either by instantiating the 'Database' class directly or indirectly.
The above example uses the 'query' and 'fetchrow' methods to do some database operations.
The database could also be instantiated explicitly as follows:
You can also use the 'siteload.php' file to load the json file 'mysitemap.json' to further automate working with the framework. This file is in the 'includes' directory. There is a 'mysitemap.json.php' file that is well commented. You can uncomment sections of this file or add items as needed.
You can run this file as a CLI program and it will output to 'stdout'. Create your 'mysitemap.json' file as follows:
Copy the created file to your project directory. There is already a 'mysitemap.json' file in the 'examples' directory.
I set the Apache2 environment variable 'SITELOAD' to point to my 'siteload.php' file in 'vendor/bartonlp/site-class/includes'. You can add it to your '/etc/apache2/apache2.conf', to an apache2 virtual host or to your '.htaccess' file. I will assume you have done this in the following examples.
This example uses the 'mysitemap.json' explicitly and converts it into an object. The next example uses 'SITELOAD'.
In addition to the SiteClass and Database classes there are several others classes in the 'database-engins' directory:
The dbTables class uses the Database class to make creating tables simple.
The 'maketable' method takes several optional arguments to help setup the table. Using the options you can give your table an id or class or set any other attributes. You can also pass a 'callback' function which can modify the rows as they are selected (see the 'example-insert-update.php' file in the 'examples' directory for more information). Also take a look at dbTables Documentation.
Using Other Libraries
You can use other frameworks or templeting engines. Here we will use Twig a popular templet engine. Twig is a super powerful templet engine with looping and conditional statements and much more. Here we do just about the minimum just as an example.
There is no 'vendor' directory in the examples directory. You will need to run 'composer' to get Twig and Altorouter installed.
The check out the 'example-with-twig.php' in the repositories 'examples' directory.
If you get an error check out 'EXAMPLES.html' in the 'examples' directory.
ReST Routing with Altorouter
If you need ReST routing (or pretty routing or SEO friendly routing as it is sometime called) you could use one of the popular routing engines available with Meteor, Laravel, Synfony2 or Silex but then again by that point you might as well just bite the bullet and spend the hours or days trying to figure out those frameworks.
There is a pretty simple router called Altorouter which can be used without too much work.
The file 'example-route.php' would normally be your 'index.php' in a production environment. When using a server with Apache2 you would need a '.htaccess' file in the directory where the 'index.php' lives. The '.htaccess' file would look like this:
This says that if the requested filename does not exist go to the 'index.php' file instead.
To test this without renaming the 'example-route.php' you can use the PHP server like this from the 'examples' directory on your server:
The PHP server uses the 'example-route.php' file and you don't need a '.htaccess' file.
Prefixing the command with 'HOME=...' sets the 'HOME' environment variable. Have it point to your 'vendor/autoload.php' file.
Now you can run the program. It will display a table and a form you can use to insert new records. Also a button lets you reset the database table to its original state. If you click on a number in the 'ID' column of the table you get an edit page where you can change the names.
You can also get to the edit page by entering the URI '/edit/3' for example. That will take you to the edit page for 'ID' three. From that page you and enter the URI '/home' which will take you back to the home page.
The advantage of ReST is you do not actually need a '/edit/3' or a '/home' directory on your system. These are just syntactical links to control logic and as a result are easily modified. Also some people think that '/edit/3' somehow looks cooler then '?edit=3', I am not sure I agree. ReST is syntactical sugar that takes more code and obfuscates what is really going on. But that is just my opinion.