Building a CO2 sensor using a Pi PicoW & automating a home ventilation system part 2
The second in a 3-part series giving a walk-through of creating a custom co2 sensor using the raspberry pi picow and hooking it up to a home ventilation system.
Posted on Wednesday 2nd July 2014
Hello, I’m back again with the 3rd installation of my MVC Framework in PHP series, again apologies that it took so long! Now without further ado, let’s get straight into it!
We left of last time having created our index page view.
What I’m going to show you today is how to render an existing page chosen, as well as start with styling our pages with bootstrap and get it looking like a real site. To start, we’re going to need to open our Init.php file in our /libs folder. Your file should more or less look the same as last time as so:
What we’re going to do is check to see if a controller exists from the URL specified by the user. If it does exist we’re going to require that file so we can then instantiate it. So right after our previous if statement we are going to write:
As stated, this will check to see if the file exists, if it does we require in that file, and then instantiate it for use. The important thing now though, is our else. If no such file exists we need to log an error to the user acknowledging this fact. We’ll come back to our Init.php file in a moment, let’s go and make an error controller.
Go to our controllers/ folder, and create a file called error.php. The structure of the file is going to look much the same as our previous controller, except for a minor difference. We’re going to pass in an argument our error class constructor so we can render it to the page.
Now, same as before go to your views folder, and create a new folder called error and inside it a file index.php. Within our index.php we’re going to check if our errors array has been set, if it has we then render our errors to the page for the user to see.
As you will see later when we test this class, if a user were to try to visit a URL that does not have a file associated with it, they will be routed to our error controller, and displayed an error accordingly. So let’s go back to our Init.php to test this out.
Outside of our constructor we are going to create a method called init_error to test. It will take a string variable as argument, require our error controller, instantiated our object and then render it. It will look like this:
Back in our constructor we can now modify the last if / else statement we made. To explain what this does: After we instantiate our object we want to check if the URL is attempting to route to a method within the specified class .It could be something like http://ciangallagher.net/aboutme/myname which might return my name as a string as an example. The full code looks like this:
So now when I try to go to an invalid URL I will see this:
The same for an invalid method:
Now I think it’s time we started added some styles to our site so we can actually start making it look like a website. The first thing you’re going to need to do is download the latest version of Bootstrap. As of writing that is version v3.2.0. It can be found here: http://getbootstrap.com/getting-started/. After you’ve done this, it’s time to change up our folder structure to accommodate for these changes. After you’ve donwloaded bootstrap, extract it, and you should see 3 folders they are your fonts, css, and js.
In your public folder of your site (which should currently be empty) i want you to make 3 new folders called css, js, images and fonts. It should look like this:
All we have to do now is extract our bootstrap files accordingly into their obvious folders `css -> css` etc. After you’ve done this we’re going to our config folder and create a file within called config.php. Within config we just need to define a base URL that can be used across the site (this can be changed as needed on live/test instances.) mine will look like this, yours may differ depending on your path.
Also, don’t forget to include this in your root index file, otherwise you won’t be able to use it! Now with that done, let’s go to our header.php and start adding our styles. We’ll start with adding our bootstrap.css. As you can see we’re using our defined URL as our path and then specify the file and location.
In our footer.php we will also include Bootstraps js file and jQuery from Googles CDN.
Now let’s go back to our header.php file and using bootstraps pre-defined mark-up http://getbootstrap.com/components/#navbar we can make ourselves a navigation bar. The full code for the header.php will look like this:
And look at that! We can now navigate between our pages, and it’s starting to look more like a site!
**Well enough writing for one day, i will hopefully be back sooner with Part 4. Check back soon! The full source code is also available on github, just follow the link below.