Web Applications Best Practices Martin Kruli by Martin

Web Applications Best Practices Martin Kruliš by Martin Kruliš (v 1. 1) 14. 1. 2016 1

Application Design � 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 by Martin Kruliš (v 1. 1) 14. 1. 2016 2

Application Design � Front Controller Design Pattern /myweb/home is rewritten to /myweb/index. php? page=home �A mod_rewrite Example Rewrite. Engine On Rewrite. Cond %{REQUEST_URI} !^/myweb/(css|pic|index. php) Rewrite. Rule ^([-a-z. A-Z 0 -9_]+)/? $ /myweb/index. php? %{QUERY_STRING}&page=$1 [L] by Martin Kruliš (v 1. 1) 14. 1. 2016 3

Application Design � 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 by Martin Kruliš (v 1. 1) 14. 1. 2016 4

Templates � 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}}, <%tag_name%> �Processed by text-replacement functions by Martin Kruliš (v 1. 1) 14. 1. 2016 5

Database � 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: : get. Instance()->get(42); by Martin Kruliš (v 1. 1) 14. 1. 2016 6

Database � Implementing Data Models ◦ Not. ORM (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"; by Martin Kruliš (v 1. 1) 14. 1. 2016 7

Application Design � 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 by Martin Kruliš (v 1. 1) 14. 1. 2016 8

Application Design � Imperative vs Declarative Approach ◦ Imperative ~ sequence of commands ◦ Declarative ~ data definitions �Declarative approach is often preferred Imperative Declarative switch ($_GET['page']) { case 'home': require 'home. php'; break; case 'settings': require 'settings. php'; break; . . . } $pages = [ 'home' => 'home. php', 'settings' => 'settings. php', . . . ]; $page = $_GET['page']; if (isset($pages[$page])) require $pages[$page]; by Martin Kruliš (v 1. 1) 14. 1. 2016 9

Application Development � Software ◦ Analysis Engineering Approach �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) by Martin Kruliš (v 1. 1) 14. 1. 2016 10

Web Crawlers and SEO � 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 <h 1>, <em>, … by Martin Kruliš (v 1. 1) 14. 1. 2016 11

Discussion by Martin Kruliš (v 1. 1) 14. 1. 2016 12
- Slides: 12