Coding a PHP 7 Framework #8 - Code Workshop

In part 8 of Coding a PHP Framework, we make a simple application by making a /view/ subfolder.

Coding a PHP 7 Framework #8 - Code Workshop

12th October 2018 in   PHP Tutorials by Elliott Barratt

You can view the finished framework at any time by visiting the GitHub repo at

If you need more space to see the code, click "Hide Sidebar >" above the Article Categories box to the right ->

Let's take our newly created framework and make a small application using it.

Application Specific Configuration

First we need to deal with our /engine/app_config.php file:

	/*Error Reporting on, turn off after deployment. */
		define('DEBUG', TRUE);
	* core definitions
		define('DEFAULT_DIRECTORY', 'public');
		define('SITE_DESCRIPTION','A small app built on the lectric PHP framework.');

First we ensure our small application is in DEBUG mode, which will ensure all PHP warnings and errors are output.

After that, we define the DEFAULT_DIRECTORY to be 'public', to correspond to /view/public/ in our directory tree. Speaking of which, ensure your /view/ directory looks like below:

  • view/
    • public/
      • render.php
      • css/
        • style.css
      • img/
      • js/
        • script.js
      • template/
        • common/
          • header.php
          • content.php
          • footer.php
        • views/
          • index.php
          • another-page.php


As we know from the previous article, the /library/Lectric/view.class.php class looks for the render.php file in any /view/ subfolder:


	* Which page is being requested?
			case 'index':
				$pageArray = [
					'meta_title' => 'Main webpage',
					'meta_description' => 'This is the main webpage of the small application website.',
					'meta_keywords' => 'main,page,small,application',
					'html' => '<h1>Main Page</h1><p>This is the main webpage. <a href="/another-page/">Click here to see another webpage.</a> <a href="/does-not-exist">Click here for a page that doesn\'t exist.</a></p>'
			case 'another-page':
				$pageArray = [
					'meta_title' => 'Another webpage',
					'meta_description' => 'This is the other webpage of the small application website.',
					'meta_keywords' => 'other,page,small,application',
					'html' => '<h1>Other Page</h1><p>This is the other webpage.</p>'
				header("HTTP/1.0 404 Not Found");
				$pageArray = [
					'meta_title' => 'Error',
					'meta_description' => 'This webpage cannot be found.',
					'meta_keywords' => '',
					'html' => '<h1>404 Not Found</h1><p>This webpage cannot be found.</p>'

	* Include each template part from directory.


In the axample of our small application, we'll use the /view/public/render.php file to set the contents of an array $pageArray based on the given request (kindly put into the property $_pageURL by the view class). In real world applications, you'd make a call to $this->loadPage() to grab a given view page from a database, but we'll keep it simple for the purposes of this article. Remember that $_pageUrl is set to 'index' when the URL_NODES constant is empty, i.e. the home page of the website.

If the request evaluates to the default clause of the switch(), we also send out a 404 header.

After that we include the file that make up our page template, header.php, content.php and footer.php, using the directory folder set by the view class: $_fileDirectory.


Our header template part uses the $pageArray to set some meta info, since this is in the same scope:

<!doctype html>
<html lang="en">

    <meta charset="utf-8" />
    <title><?php echo $pageArray['meta_title'].' | '.SITE_NAME; ?></title>

    <!--content meta information-->
    <meta name="description" content="<?php echo $pageArray['meta_description'];?>" />
    <meta name="keywords" content="<?php echo $pageArray['meta_keywords'];?>" />


In our content file, we check for the existence of the equivalently name php file in our /view/public/template/views/ folder, and include it if found:


	* Include the webpage file if exists 

		if (file_exists(DOC_ROOT.'/view/'.$this->_fileDirectory.'/template/views/'.$this->_pageUrl.'.php')) {




And to close it all off:

<!--add your custom JS (e.g. Google Analytics code etc) here-->



Page Specific View Files

For this example, the content of /view/public/template/views/index.php and /view/public/template/views/another-page.php are the same:


    echo $pageArray['html'];


That's how simple it is to get a functional, fast, pretty-url application up and running. Of course, your template files could and should include more meta info, links to external resources like css and js, but the concept remains the same.

If you want to know how to do more complex operations, see the gitHub repository .readme for more information. In future articles we'll explore how to use the /do/ request features to implement some common web application features like form submission and ajax requests.

Continue to Part 9 by clicking here.

Hide Sidebar >



Hide Sidebar >

This website uses cookies. Privacy Policy