1stwebdesigner |
Creating Your Own CMS Part 3 – Advanced Features Posted: 04 Apr 2011 03:00 AM PDT If you have been following along you should have a simple CMS built that has a place to securely log in and you should be able to create some users and access levels for your site. If you are creating a CMS you will need more than just to be able to log in and create users so here in part three we will get into a few of the things that you can do with your own CMS. A few people have really knocked the idea of creating your own CMS which is fine because we can all have our own opinions so I will just tell you all why I personally like to use my own.
Reasons You Might Build Your Own CMSFirst off, the main reason is that you have 100% control. Yes, it can be a lot of work, but if you plan to have multiple sites then it is awesome to have your own CMS that you can easily fully customize to whatever you want. Secondly, I did this to further sharpen my PHP, JS, AJAX, Jquery, CSS, and HTML skills. The company that I work for does not, and will not, ever use open source CMS or even a retail CMS. There are several ups and downs to using a common CMS. The ups are that you can easily find people to help you work on things with it and one of the main downsides is if there is any sort of flaw or security issue then everyone will know about it quickly and you can be vulnerable to attacks until it is fixed. Adding Content To Your CMSTo kick this off I would like to talk about the front end structure of your site first. If you want to be able to easily add content on the fly to your site then the layout has to allow for it. Unless you create a CMS that builds the entire page’s HTML files. The example that I plan to show you does not show building entire HTML pages, but it can easily be altered to do so. The basic setup of the content manager that I am showing you guys is going to require a layout that is flexible but simple and a little bit of work from the .htaccess. The Back End For Adding ContentThe actual back end itself is very basic but depending on your site and features it can very well be more complex if you like. You simply start with a form in your CMS to add/edit/delete content pages. This form will need at the very least a place for a url and a place to add content. Technically you do not need the url you could simply do a ?id=84 with the 84 just being an auto increment from the database. Obviously this is a pretty ugly way to do it so we will assign URLs to each. Along with a URL and content I always have at least a page title, meta keywords, and meta description but regardless of what you have it is all basically the same. What You Need On The Front EndIf you have made it this far along in the tutorials I assume you probably do not need any help with how to make this form so I will skip those details. However, I will just mention that each of the new content pages that you add with this form will need to be stored in a database. I do it with an auto increment id field for the PRIMARY key and also add an INDEX on the url field because we will be using that field to look up pages. From there you can handle the front end in a few different ways. You can go back to my flat file caching and cache out each content page as an array using the url as the key for each element so that it is easy to reference. I have done things like this in the past but don’t usually anymore as the benefits are really more opinion/preference than performance in many situations. Another option, and the one I most commonly use, would be to simply take the url and look up the page in the database then display the content. When done this way I like to have a header.php, header.init.php, and of course my index.php and footer.php. The header.init will be where I have the code to parse out the URL and try to find the content page in the database. The header is mainly just some html that contains the header area of my website and the index is the middle content area of my site that will display content per the given URL. You will also have a .htaccess file to handle redirects. .htaccess RewriteEngine On RewriteCond %{HTTP_HOST} ^bbillman.com$ [NC] RewriteRule ^(.*)$ http://www.bbillman.com/$1 [R=301,L] RewriteCond %{HTTP_HOST} ^([www\.]?)bbillman.com$ [NC] RewriteRule ^(.*)$ http://www.bbillman.com/$1 [R=301,L] RewriteRule ^tmp(.*)$ /admin/tmp_image.php [NC,L] RewriteRule ^(.*)(\.(jpg|jpeg|gif|png|css|ico|xml|txt))$ - [NC,L] RewriteRule ^$ - [NC,L] #RewriteCond %{DOCUMENT_ROOT}/$1 !-f RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ /$1/ [NC,L,R=301] RewriteCond %{DOCUMENT_ROOT}/$1 !-f RewriteRule ^(.*)$ index.php?uri=$1 [QSA] The first line is obvious, it turns the engine on, the next few will make sure www is always at the front of the url, the third block will basically tell it to ignore the rest of the htaccess if it is an image being accessed, the fourth is saying to first look for the specified file, and the end is like a catch-all where it will send anything to index.php with a query string of uri=$1 ($1 will be whatever is after the / in the url). header.init.php $v) { if(is_array($v)) return is_empty($v); elseif(isset($v) || $v!='') return false; } } else { if(isset($array) || $array!='') return false; else return true; } return true; } if(is_array($parts) && !is_empty($parts)){ $rparts = array_reverse($parts); foreach($rparts as $v){ $p_q = $db->query("SELECT url,content,meta_title,meta_keywords,meta_description,parent_id FROM content WHERE seo_url='$v' AND live='1' LIMIT 1"); if($db->num_rows($p_q)){ $_PAGE = $db->fetch_assoc($p_q); if(is_array($_PAGE)){ foreach($_PAGE as $k => $sv) $_PAGE[$k] = stripslashes($sv); } break; } } }else{ $p_q = $db->query("SELECT url,content,meta_title,meta_keywords,meta_description FROM content WHERE seo_url='' AND live='1' LIMIT 1"); $_PAGE = $db->fetch_assoc($p_q); if(is_array($_PAGE)){ foreach($_PAGE as $k => $sv) $_PAGE[$k] = stripslashes($sv); } } if(!is_array($_PAGE)){ //404 } ?> The code is pretty simple. It just takes the uri and breaks it up into an array and starting from the back it searches the database for a matching url. If the url exists then it will put all of the contents for that page into an array $_PAGE and if not then it will try to grab the home page (where url=”) and if there is no page then you can have it do a 404. In the header of course you would set the meta data and have whatever other design stuff you need for your site in there. index.php <?php include'includes/header.php'; echo $_PAGE['content']; include'includes/footer.php'; ?> Using this method your index.php could be as simple as this. It will include the header and footer and just output the content of the requested URL in the middle. Conclusion To Creating Your Own CMS Part 3In conclusion, this is an easy and effective way to make adding content quick and easy. This can also be done for blogs actually and done almost exactly the same way. One thing is you would have to add a rule in your htaccess to send URLs with /blog/ to a separate blog.php or add some more code to your index.php, but I prefer to have them separate for readability and such. Likewise this can be used for events or pretty much any other content that you would like to add to your site. Adding new content is obviously important as it will help you to get some SEO built up because the internet is all about information. |
You are subscribed to email updates from 1stwebdesigner - Graphic and Web Design Blog To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
Comments (0)
Post a Comment