Basic Development Example


    Ok, let's get to some nitty-gritty. You've got A Bright CMS installed and ready to go and you want to add something to your website. Let's say you'd like to add an about page with a list.

    Navigate to application/controllers/example.php and find/replace all instances of "Example" with "About". Then rename "example.php" to "about.php". Optionally, you could create a new about.php file and copy and paste the contents of example.php and then perform the find/replace action. Do the same with "exampleView.php" in application/views and "exampleModel" in application/models.

    Then, create a new file named about.json and add it to your dev/json directory. In this file, create a title and a description like so:

    1. {
    2. "title" : "About",
    3. "description" : "Holds the content and metadata for our About page"
    4. }

    Underneath description, we can add a content section, and then our list like so:

    1. "content" : {
    2. "list" : {
    3. "Hello",
    4. "World",
    5. "This",
    6. "Page",
    7. "Rocks"
    8. }
    9. }

    Next we need to create a method in our About controller that will allow us to feed our data into the view. Such a method might look like the following:

    1. private function _setAboutList($list_data)
    2. {
    3. $this->_view->about_list = null;
    4. foreach ($list_data as $list_item)
    5. {
    6. $this->_view->about_list .= $this->_view->buildAboutList($list_item);
    7. }
    8. }

    Now we need to create the "buildAboutList" method in our view. Navigate to the "aboutView.php" we created earlier and enter the following in our class:

    1. public function buildAboutList($list_item)
    2. {
    3. return '<li>' . $list_item . '</li>';
    4. }

    Let's go back to our About controller. We want to make sure that the private method we added in our controller is called when the page is loaded. All page controllers should have a "_pageBuilder" that calls all the methods necessary to render our view. If we kept the code from the example.php file, it should look something like this:

    1. protected function _pageBuilder($data, $cache_buster = null)
    2. {
    3. $this
    4. ->_setViewProperty('title_page', $data['template']['head']['title_page'][strtolower(__CLASS__)]['text'])
    5. ->_setViewProperty('example_content', $data['example']['content']);
    6. return parent::_pageBuilder($data, $cache_buster);
    7. }

    Add the following line of code after "$this" and above the first method call:

    1. ->_setAboutList($data['about']['content']['list'])

    Our data is being passed to the "_pageBuilder" from the "index" already, so all we need to do is point to the part of the array that contains our list data.

    The last thing we must do, is add the "$this->_view->about_list" property to a view page. Go to application/views and change the "example" to "about", or copy the contents to a new "about" folder. Then load the index.php file and add the following line inside the container div:

    1. <ul><?php echo $this->about_list; ?></ul>

    If we wish to add an "About" page to the interface with the proper metadata, we must add it to the template.json and header.json files. In the template.json file, look for the "title_page" section under the "head" section. Either change the "example" reference to "about" or add it like so:

    1. "about" : {
    2. "text" : "About"
    3. "submenu" : ""
    4. }

    Finally, in header.json, under the "header_nav" section, change the "example" section or add the following:

    1. "about" : {
    2. "text" : "About"
    3. "is_anchor" : "1"
    4. "is_internal" : "1"
    5. "path" : ""
    6. "target" : "_self"
    7. "title" : ""
    8. "id" : ""
    9. "class" : ""
    10. }

    Congratulations! Now when you refresh the website, you should see an "About" page that will display your list when you navigate to it. We have now cleanly separated our content, our view pages, our view methods, and our controller methods. Changing our content, how it is displayed, or using these techniques as the basis of something more complex has been made clean and maintainable.