Download presentation
Presentation is loading. Please wait.
Published byByron Gordon Modified over 9 years ago
1
Martin Kruliš 14. 1. 2016 by Martin Kruliš (v1.1)1
2
Front Controller Design Pattern ◦ Application has a single point of entry ( index.php ) All requests are directed to this script (bootstrap) E.g., using mod_rewrite in Apache configuration ◦ Bootstrap script ensures routing and dispatching Routing – selection of target class (routine, method, …) Dispatching – invocation of target (loading script, …) Different handling for GET and POST requests Advantages ◦ More secure (only one gate to fortify) ◦ Less error-prone for programmers 14. 1. 2016 by Martin Kruliš (v1.1)2
3
Front Controller Design Pattern A mod_rewrite Example RewriteEngine On RewriteCond %{REQUEST_URI} !^/myweb/(css|pic|index\.php) RewriteRule ^([-a-zA-Z0-9_]+)/?$ /myweb/index.php?%{QUERY_STRING}&page=$1[L] 14. 1. 2016 by Martin Kruliš (v1.1)3 /myweb/home is rewritten to /myweb/index.php?page=home /myweb/home is rewritten to /myweb/index.php?page=home
4
Model-View-Controller Design Pattern ◦ For showing/processing individual pages ◦ View Part that covers HTML rendering Presents data from Model ◦ Model API for data representation and manipulation ◦ Controller Business logic Triggers all actions Handles user’s feedback 14. 1. 2016 by Martin Kruliš (v1.1)4
5
Idea of Templates ◦ Separate HTML (CSS, …) code from PHP scripts Division of work (HTML coders vs. PHP programmers) Template Systems ◦ PHP-based Template is also a PHP script PHP-template only includes data into the HTML ◦ Text-based Special tags in HTML {{tag_name}}, Processed by text-replacement functions 14. 1. 2016 by Martin Kruliš (v1.1)5
6
Implementing Data Models ◦ Direct SQL writing is inconvenient Better to use some data abstraction layer ◦ Object-relational Mapping (ORM) Tables are mapped to classes or singleton objects Rows are mapped to objects (constructed by tables) The corresponding classes has to be generated from the database schema (or vice versa) SELECT * FROM users WHERE id = 42; $user = Users::getInstance()->get(42); 14. 1. 2016 by Martin Kruliš (v1.1)6
7
Implementing Data Models ◦ NotORM (by Jakub Vrána) Keeping classes and DB schema in sync is very tedious in ORM systems Another approach is to use universal object mapping using dynamic features of PHP $users = $db->users() ->select("id, login, name") ->where("active", true) ->order("name"); foreach ($users as $id => $user) echo $user["name"], "\n"; 14. 1. 2016 by Martin Kruliš (v1.1)7
8
Dependency Injection ◦ Software design pattern Principles of component-based programming ◦ Removes hard-wired dependencies from the code Decoupling the code Make it more coherent, robust, and less error-prone ◦ Dependent (consumer) declares list of dependencies As interface contracts (services it requires) ◦ Injector (provider) creates instances of requested services and provide them to the consumer I.e., injector is responsible for assembling components 14. 1. 2016 by Martin Kruliš (v1.1)8
9
Imperative vs Declarative Approach ◦ Imperative ~ sequence of commands ◦ Declarative ~ data definitions Declarative approach is often preferred 14. 1. 2016 by Martin Kruliš (v1.1)9 Imperative switch ($_GET['page']) { case 'home': require 'home.php'; break; case 'settings': require 'settings.php'; break;... } Declarative $pages = [ 'home' => 'home.php', 'settings' => 'settings.php',... ]; $page = $_GET['page']; if (isset($pages[$page])) require $pages[$page];
10
Software Engineering Approach ◦ Analysis Gathering/anticipating user requirements Pay extra attention to scaling problems ◦ Development Use appropriate scope Trivial inline PHP for trivial applications, robust frameworks and design patterns for complex applications ◦ Testing User/Application Testing (e.g., Selenium) Unit testing (e.g., PHPUnit) Continuous Integration (e.g., Travis CI) 14. 1. 2016 by Martin Kruliš (v1.1)10
11
Web Crawlers ◦ Automatons that search the web Follow the links they find ◦ Configured by robots.txt in the root of the web See http://www.robotstxt.org/ for details Search Engine Optimization (SEO) ◦ URL is very important Keywords should be also in URL ◦ Meta-tags (description, keywords) ◦ Correct usage of tags that mark significant content Especially,, … 14. 1. 2016 by Martin Kruliš (v1.1)11
12
14. 1. 2016 by Martin Kruliš (v1.1)12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.