Planning our Framework [ 58 ] .htaccess file We have our index.php le set up to process the incoming request and send it to the relevant controller. However, URLs which have the format of index. php?page=some/page/on/our/site or index.php?page=products/view/some- product are not as attractive or memorable as those with just some/page/on/our/ site or products/view/some-product. With the Apache module mod_rewrite we can get our site to rewrite the more friendly URLs into the less friendly ones for our framework to understand. ErrorDocument 404 /index.php DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?page=$1 [L,QSA] </IfModule> This .htaccess le instructs the web server (Apache) to use index.php as the index le within a directory. It also instructs Apache that if the mod_rewrite module is enabled, the requests that are not for valid les or directories should be rewritten to the main index.php le. However, anything that occurs after the directory containing the .htaccess le, should be appended to the page $_GET variable. For example, oursite.com/pagea would be rewritten to oursite.com/index. php?page=pagea. Configuration file We also need a conguration le, to store our database connection settings. Most other settings will be stored in the database. However, the actual connection details cannot be stored within it, as we need to know the details before we connect to the database; otherwise, we cannot connect. <?php $configs = array(); $configs['db_host_ecomframe'] = 'localhost'; $configs['db_user_ecomframe'] = 'root'; $configs['db_pass_ecomframe'] = ''; $configs['db_name_ecomframe'] = 'phpecommerce'; ?> This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 2 [ 59 ] The simplest way to store the settings is within an array in a conguration le, with the keys of the array relating to what the value is used for. The sufx of ecomframe is used to allow us to store multiple database connection details within the same array. What about e-commerce? Looking at frameworks is important; however, we haven't really covered anything specic to e-commerce yet. So where does e-commerce t into this? Most of our e-commerce functionality ts into this by adding models and controllers that perform the relevant e-commerce tasks we require, such as managing products, handling the order processing and checkout process, and so on. An e-commerce registry? One thing that we could consider is an e-commerce registry. We could perhaps have our shopping basket as a registry, containing a collection of products. This would make it simple for each page to access the shopping basket and return the number of items contained within. For the framework we are going to create in this book, we are not going to use this method. However, it may be something you wish to think about with your own framework. After saying that, you may be wondering how we are going to provide access to the basket to all areas of the framework. The answer is with some call backs. At certain key points of execution within the framework, certain functions will be called. Inside this, we provide code to interact with the shopping basket. Exactly where these callback functions are, and when they are called, is a discussion for another chapter. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Planning our Framework [ 60 ] Summary In this chapter, we created a sound start to our framework which will be extended with e-commerce functionality during the rest of this book. We specically covered: Various software architectural and design patterns, looking into best practices for implementing certain aspects of our framework Developing a directory structure for our framework Creating a registry to store core objects How we will use the MVC pattern to structure and operate our framework Routing page requests around our framework and an overview of routers Creating our single point of contact for accessing the framework, as well as an htaccess and conguration le Next we move onto storing, displaying, and managing products and their categories, some true e-commerce functionality! • • • • • • This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Products and Categories With a basic structure to our framework in place, we can now start to think about the e-commerce aspects to it. In this chapter you will learn: How to structure content within the framework, including: Page structure Product structure Categories structure How to access and display products and categories with models and controllers How to design views to interact with our framework As we discussed in Chapter 1, PHP e-commerce, Juniper Theatricals requires a framework to power their website as well as the e-commerce features, so page management is a must. What we need Before we start building products and categories into our framework, let's think about what information we need, both to display to our customers and for the use of the store administrator. To provide our customers with sufcient product information, we need to inform them of the name of the product, a detailed description of the product, and the price of the product. We may also wish to show them a photograph of the product and a number of additional images related to the product. Additionally, we may wish to make them aware of the weight and the cost of shipping, number of items we have in stock, as well as any categories the product is contained within. From an administration perspective, we need a reference number, or ID number, and we may need a Stock Keeping Unit reference. We may also require a search engine friendly name, which is used within the URL to view the product. • ° ° ° • • This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Products and Categories [ 62 ] Product information Taking into account what we have just discussed, at a minimum we need to store the following information: Data Description ID A reference number for the framework to reference the product Name The name of the product Search Engine Friendly Name A search engine friendly name for the product to be displayed in URLs. Description A detailed description of the product SKU A stock keeping reference (usually supplier's reference, or for integration with stock keeping systems) Price The cost of the product Stock The number of these products which are currently in stock Primary image An image of the product Additional images A number of additional images which are displayed as thumbnails and then toggled into the place of the main page Shipping costs and information will be discussed in Chapter 8, Shipping and Taxes, so we don't need to take those aspects into consideration just yet. Category information We need to be able to contain our products within categories, so what information would we need to collect for our categories? Data Description ID A reference number for the framework to reference the category Name The name of the category Description A detailed description of the category Search Engine Friendly Name A search engine friendly name for the category, to be used for display within URLs. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 . array. What about e-commerce? Looking at frameworks is important; however, we haven't really covered anything specic to e-commerce yet. So where does e-commerce t into this? Most of our e-commerce. /index .php DirectoryIndex index .php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index .php? page=$1. ''; $configs['db_name_ecomframe'] = 'phpecommerce'; ?> This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 2 [ 59 ] The simplest