Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Applications Best Practices

Similar presentations


Presentation on theme: "Web Applications Best Practices"— Presentation transcript:

1 Web Applications Best Practices
Martin Kruliš by Martin Kruliš (v1.2)

2 Application Design Front Controller Design Pattern Advantages
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š (v1.2)

3 Can be optimized further with classes and annotations
Front Controller Imperative vs Declarative Approach Imperative ~ sequence of commands Declarative ~ data definitions Declarative approach is often preferred Can be optimized further with classes and annotations Imperative switch ($_GET['page']) { case 'home': require 'home.php'; break; case 'settings': require 'settings.php'; ... } Declarative $pages = [ 'home' => 'home.php', 'settings' => 'settings.php', ... ]; $page = $_GET['page']; if (isset($pages[$page])) require $pages[$page]; Example by Martin Kruliš (v1.2)

4 Templates Idea of Templates Template Systems
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 Check out Latte templating system in Nette framework. It is a quite neat example of templating. Example by Martin Kruliš (v1.2)

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::getInstance()->get(42); For ORM example check out Doctrine ( by Martin Kruliš (v1.2)

6 Database 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"; For NotORM chek out or dababase API in Nette. by Martin Kruliš (v1.2)

7 Programming Practices
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š (v1.2)

8 Programming Practices
Dependency Injection Example – database connection service Centralized Solution $db = mysqli_connect(...); ... function get_users() { global $db; mysqli_query($db, ...); } DI Solution $db = mysqli_connect(...); ... function get_users($db) { mysqli_query($db, ...); } DI becomes even more important and interesting when objects and classes are used by Martin Kruliš (v1.2)

9 Application Development
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) by Martin Kruliš (v1.2)

10 Discussion by Martin Kruliš (v1.2)


Download ppt "Web Applications Best Practices"

Similar presentations


Ads by Google