1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5 e-commerce Development- P19 pptx

5 72 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 280,31 KB

Nội dung

Chapter 3 [ 73 ] View The view for a page should consist of the following template les: Header: To have a common header used on most pages and areas of the website. Footer: To have a common footer used on most pages and areas of the website. Page: To display the page to the user. We will also need some additional templates, depending on the page itself, including: 404 error template: To indicate that the page was not found. Login template: For secure pages where the current user is not logged in. "Page disabled" page: For inactive pages, although we may of course prefer to generate a 404 error page, so that the visitor does not realize that the page they tried to view exists, but is just disabled. Header template Our header template will contain the rst bit of HTML for a particular page, so obviously this needs to contain our doctype, page title (empty as this is populated later), style references, and so on. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>{title}</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <meta name="description" content="{metadescription}" /> <meta name="keywords" content="{metakeywords}" /> </head> <body> Footer template The footer template is for content and HTML, which is displayed after a page, product, or other primary area of the site. For now, this just needs to close our <body> and <html> tags. </body> </html> • • • • • • 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 [ 74 ] Page template The page template itself needs very little, just a heading and some content. The content is stored in the database as full HTML, so content can be marked up and then displayed in the page. <h1>{pageheading}</h1> {pagecontent} 404 error template Content for our error template would just be the following: <h1>Page not found</h1> <p>Sorry, we could not find the page or file you were looking for, please return to the home page and try again.</p> <! 404 error > Other templates We may also wish to have a login template and a "Page disabled" template. These are the aspects we will focus on a little more in our framework's development. Controller The controller needs to be able to: Clean the path requested (to prevent any security issues, for example MySQL injection) Pass the path to the page model If the page is valid, convert the page properties into tags for use in the template system, and output the page view If the page is not valid, display the 404 error view If the page is valid and secure, display the page if the visitor is a logged-in user, or else display a login page. This gives us the following controller: <?php // controllers/pages/controller.php class Pagecontroller { private $registry; • • • • • This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 3 [ 75 ] Our constructor receives the registry as a parameter so that the object can use the registry when it needs to. It also receives a Boolean value for $directCall to indicate if the controller should assume the user is accessing the controller, or another controller is piggy-backing on this one, to call some of its functions. public function __construct( PHPEcommerceFrameworkRegistry $registry, $directCall ) { $this->registry = $registry; if( $directCall == true ) { $this->viewPage(); } } Assuming the user is trying to view a page (at present, that is all this controller supports!), we must lookup the page using the page model, generate the view, and insert the relevant data. private function viewPage() { // We require the page model, so we create a new page model // passing the URL path as a reference to allow it to look up the // page. require_once( FRAMEWORK_PATH . 'models/page/model.php'); // Page model needs different class name, as page is used for // the template handler! $this->model = new Pagemodel( $this->registry, $this->registry->getURLPath() ); // If the page is valid, or not valid, the relevant templates are // displayed, and if appropriate, the pages data fields are // assigned to template variables, to display in the view. if( $this->model->isValid() ) { $pageData = $this->model->getProperties(); $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'main.tpl.php', 'footer.tpl.php'); $this->registry->getObject('template')-> dataToTags( $pageData, '' ); $this->registry->getObject('template')->getPage()-> setTitle( $pageData['title'] ); } else { $this->pageNotFound(); } } 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 [ 76 ] If the page isn't found, we need to display a suitable error message. private function pageNotFound() { $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', '404.tpl.php', 'footer.tpl.php'); } Our framework should also have scope to expand for pages that are disabled, or require the user to be logged in (and the user isn't logged in), thus displaying a login page. private function pageRequiresLogin() { // TODO } private function pageDisabled() { // TODO } } ?> Products Building functionality for our products can be broken down into three main areas. Firstly we need to create a model, which represents a product in our code. In time we will extend this model to be able to easily update and save individual products as well as clone them, but for now it will just represent a product's data. Secondly we need a controller, to interpret the user's page request, interface with the model, and work with the view to display the data to the end user. Finally we need a view or a series of templates, to display the information to the end user. Model At this stage in our framework's development the model has a very simple function: to query the database for a specied product, and represent the data for the product in object form. It also needs to determine if a product is valid or not, so that our constructor can either display the product or an error message (for example invalid product) to the end user. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 3 [ 77 ] To fully understand the main lookup query this model must perform, let us reect on our database structure, which has been developed previously within the chapter. First we have our content table; from here we lookup the product's unique search engine friendly name, as the path to the content element within this table. We also need to ensure that the content element is set to be active. The next stage is to check that the type of content is that of a product, by referring to the content_types table. Then we need to look up the content_versions table, to get the main content such as the name of the product, and a description of the product. Finally, we need to link to the products table itself, to get the product-specic data such as price, weight, and so on. The following query nicely takes care of that for us: SELECT v.name AS product_name, c.ID AS product_id, p.image AS product_image, p.weight AS product_weight, p.price AS product_price, p.SKU AS product_sku, p.featured AS product_featured, v.heading AS product_heading, v.content AS product_description, v.metakeywords AS metakeywords, v.metarobots AS metarobots, v.metadescription AS metadescription FROM content_versions v, content c, content_types t, content_types_products p WHERE c.active=1 AND c.secure=0 AND c.type=t.ID AND t.reference='product' AND p.content_version=v.ID AND v.ID=c.current_revision AND c.path='{$productPath}' At this stage, the model needs the following distinct functionality: Lookup a product based on a product path Determine if a product exists or not, based on said path Store product data Return product data as an array Return a Boolean value indicating if a product is valid or not Further into our framework development, we will want and need to extend this quite a lot, but for simply representing and displaying products, this will sufce for now. <?php class Product{ private $registry; private $ID; private $name; private $SKU; private $description; private $price; private $weight; private $image; private $stock; private $heading; • • • • • This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 . $this->registry->getObject('template')-> buildFromTemplates('header.tpl .php& apos;, 'main.tpl .php& apos;, 'footer.tpl .php& apos;); $this->registry->getObject('template')->. $this->registry->getObject('template')-> buildFromTemplates('header.tpl .php& apos;, '404.tpl .php& apos;, 'footer.tpl .php& apos;); } Our framework should also have scope to expand for pages. user, or else display a login page. This gives us the following controller: < ?php // controllers/pages/controller .php class Pagecontroller { private $registry; • • • • • This material is copyright

Ngày đăng: 07/07/2014, 10:20