Creating a semi-static website with CodeIgniter ROUND 2
Published on Monday, 06 June 2011.
I'm a bit late with this article (just about 4 months xD).
The use case for a site like this is very limited and I don't think it's very common, but here's another way to use CodeIgniter for a static website. This article builds upon the previous related article Creating a semi-static website with CodeIgniter.
1. The controller
With this approach, we would use another controller: applications/controllers/staticx.php
2. The configuration
And a different configuration for the application/config/routes.php file.
3. A common layout
Almost finally, for this to work, a file named layout.php must exist inside the application/views/ directory.
A quick example:
4. Adding a page
Now, adding a new page to your static site is just a matter of creating the view file inside the application/views/ directory.
Examples:
If you request example.com/about, the controller will look for the file application/views/about.php
If you request example.com/products/electronics, the controller will look for the file in application/views/products/electronics.php.
So to summarize:
| Request | View file to be rendered |
|---|---|
| application/views/about.php | |
| example.com/products/electronics | application/views/products/electronics.php |
The following is an example view file:
You can notice some PHP comments at the beginning of the file. The controller parses those comments and converts them into variables that can be used in your application/views.layout.php file. You could modify the controller to include more variables (see get_available_page_data()). This is definitely not very optimized. But I think it can work for simple static websites which are not very common nowadays. :)