Hello World - My first Application

First Steps

Every software development tutorial starts with the ultimative Hello World programm, isn't it?

Ok, let's start like this with Pletfix:

  1. Install a fresh Pletfix Application.

  2. Open boot/routes.php and add a new route:

    $route->get('welcome', function() {
       return 'Hello World';      
    });
  3. Open your browser and enter the URL to your new route:

    http://localhost/myapp/public/welcome

VoilĂ ! You will see: "Hello World". What a surprise!

Create a View

Pletfix is a MVC (Model-View-Controller) framework, so we will use a View for the greetings output.

  1. Create a new file welcome.blade.php in the subfolder resources/views and add the following content:

    @extends('app')
    
    @section('content')
    
        <h1>Hello World</h1>
    
    @endsection
  2. Modify the route in boot/routes.php so that the view is called and the output string is not returned directly:

    $route->get('welcome', function() {
        return view('welcome');
    });

Reload the browser and be happy.

Create a Controller

Our application is really simple - so it is ok adding the logic into the route file directly. But as soon as more logic is added, it becomes quickly confusing. To prevent this let's add a controller:

  1. Create a new Controller WelcomeController in app/Controllers/WelcomeController.php:

    <?php
    
    namespace App\Controllers;
    
    class WelcomeController extends Controller
    {
        public function index()
        {
            return view('welcome');
        }
    }
  2. We modify the route once more so our controller is called:

    $route->get('welcome', 'WelcomeController@index');

Reload the browser. The output is the same as the last time.

Create a Table

The next step for this tutorial is creating a table so that we can dynamically read the content of the view from the database.

By default, the Pletfix Application Skeleton creates a SQLite database, so we don't have to change the configuration. You may check the database configuration in config/database.php are correctly.

  1. Create a new migration file 20171007141500_CreatePagesTable.php in the folder resources/migrations: Note that normally you should use the current timestamp as prefix for the filename but for this tutorial it does not matter.

    <?php
    
    use Core\Services\Contracts\Database;
    use Core\Services\Contracts\Migration;
    
    class CreatePagesTable implements Migration
    {
        /**
         * @inheritdoc
         */
        public function up(Database $db)
        {
            $db->schema()->createTable('pages', [
                'id'      => ['type' => 'identity'],
                'name'    => ['type' => 'string'],
                'content' => ['type' => 'text'],
            ]);
    
            $db->table('pages')->insert([
                'name'    => 'welcome',
                'content' => 'Hello World',
            ]);
        }
    
        /**
         * @inheritdoc
         */
        public function down(Database $db)
        {
            $db->schema()->dropTable('pages');
        }
    }
  2. Execute the Pletfix migrate command:

    php console migrate  

    Now we have a new table pages with one record.

  3. Modify the index method of the WelcomeController like below:

    public function index()
    {
        $page = database()->table('pages')->where('name', 'welcome')->first();
    
        return view('welcome', ['conent' => $page['content']]);
    }
  4. Change set content section of the view:

    @section('content')
    
        <h1>{{$content}}</h1>
    
    @endsection

Reload the browser. The output is still the same as the last time, but the content comes from our database!

Create a Model

The final step of this tutorial is using a Model to access the database table.

  1. Create a new Model Page in app/Models/Page.php:

    <?php
    
    namespace App\Models;
    
    use Core\Models\Model;
    
    /**
     * @property integer $id
     * @property string $name
     * @property string $content
     */
    class Page extends Model
    {
    }

    Note that Pletfix assigns the model automatically, as long as the model is in the singular and the database table in the plural.

  2. Modify the index method of the WelcomeController ones more like below:

    public function index()
    {
        /** @var \App\Models\Page $page */
        $page = \App\Models\Page::where('name', 'welcome')->first();
    
        return view('welcome', ['conent' => $page->content]);
    }

Reload the browser. Hello World!


(edit on GitHub)