User Tools

Site Tools


getting_started

This is an old revision of the document!


Getting started with Platform

Background

Platform is a PHP framework developed by Michael Sahl. The framework is intended to make development of PHP applications very fast, and making it easy to implement new ideas. At the same time the framework is intended to be very basic and easy to understand, in opposition to most modern PHP frameworks which is abstract and have a considerable learning curve.

Architecture of platform apps

Platform application consist of one or more instances which are self-contained instances of the application, each having their own database and file structure. A simple application targeted at single users, could be a single instance, while a complex app targeted at companies and their employees could implement an instance for each company.

Installing platform

Copy the code into your website folder and edit config.php to fit your needs.

$platform_configuration = array(
    'global_database_server' => 'DATABASE SERVER NAME',
    'global_database_username' => 'USERNAME FOR DATABASE',
    'global_database_password' => 'PASSWORD FOR DATABASE',
    'global_database_name' => 'NAME FOR GLOBAL DATABASE',
 
    'instance_database_name' => 'NAME FOR INSTANCE DATABASES',
 
    'dir_store' => 'PATH TO STORAGE FOLDER',
    'dir_temp' => 'PATH TO TEMP FOLDER',
 
    'password_salt' => 'SALT TO USE FOR PASSWORDS'
);

The database user should have full right including rights to create and drop databases as it is expected to do that when creating instances.

Folders should be writable by the web server user and the cron job user.

When finished save the file and go to https://YOURSERVERNAME/install/index.php and Platform will create basic database structures.

Now you are ready to start coding!

Demo example

Navigate to https://YOURSERVERNAME/demo/ to see a quick demo of Platform and get a feeling of the instance feature. Select “Create instance” and fill the form and click “Create instance” again.

A new instance have now been created with a test user. Observe that a new database is created. You can log out of this instance and into it again. You can also create further instances if you want to, or destroy the instances you have already created.

Source code walkthrough

We start in /demo/create/index.php which is the code that creates a new instance.

  1. <?php
  2. include $_SERVER['DOCUMENT_ROOT'].'Platform/include.php';
  3.  
  4. pagestart('Create new instance');
  5.  
  6. $new_instance_form = new \Platform\Form('new_instance_form', 'new_instance.frm');
  7.  
  8. $new_instance_form->addValidationFunction(function($new_instance_form) {
  9. // Check if instance if taken
  10. if (\Platform\Instance::getByTitle($_POST['instancetitle'])) {
  11. $new_instance_form->getFieldByName('instancetitle')->triggerError('Instance name already in use');
  12. return false;
  13. }
  14. return true;
  15. });
  16.  
  17. if ($new_instance_form->isSubmitted() && $new_instance_form->validate()) {
  18. $values = $new_instance_form->getValues();
  19. $instance = \Platform\Instance::initialize($values['instancetitle'], $values['username'], $values['password']);
  20. if ($instance instanceof \Platform\Instance) {
  21. // Instance was created. Login and continue.
  22. $instance->activate();
  23. $loginresult = \Platform\User::tryLogin($values['username'], $values['password']);
  24. if ($loginresult) {
  25. header('location: /demo/app/');
  26. }
  27. $new_instance_form->getFieldByName('instancetitle')->triggerError('Instance was created, but a login couldn\'t be performed.');
  28. } else {
  29. $new_instance_form->getFieldByName('instancetitle')->triggerError('A new instance couldn\'t be initialized!');
  30. }
  31. }
  32.  
  33. echo '<div class="w3-container w3-teal">';
  34. echo '<h1>Create instance</h1>';
  35. echo '</div>';
  36.  
  37. echo '<div class="w3-container">';
  38. $new_instance_form->render();
  39. echo '</div>';
  40.  
  41. echo '<div class="w3-container w3-gray" style="font-style: italic; font-size: 0.8em;">';
  42. echo 'Platform';
  43. echo '</div>';
  44.  
  45. pageend();

Line 4 outputs the start of the page including the starting html-tag, the head-section and the starting body tag. See Design class for more info.

In line 6 we create a form to create the instance, which is easily accomplished using the special form file format. In line 8 we add an additional validation function to the form, which check that the name of the instance isn't already used, and triggers an error on the appropriate form field, if this is the case.

In line 17 we check for a form submission and validates the form. If the form is submitted and is valid, we initializes a new instance in line 19, activate it (22) and performs a login (23). If all of this is successful the instance is created and the user is logged in, so we redirect to the app.

See more in the Form class and the Instance class.

The rest of the page is just layout, with the form being outputted in line 39.

getting_started.1569680908.txt.gz · Last modified: 2019/09/28 14:28 by sahl

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki