Skip to content

Get started

yuri.blanc edited this page Aug 22, 2017 · 17 revisions

Let's go!

with composer

 composer require yuxblank/phackp 

or get phackp-bone (provides a basic project stub)

composer require yuxblank/phackp-bone

Application setup

if you're using apache http write a .htaccess file on the root with:


Options -MultiViews

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^ index.php [QSA,L]

Create a index.php file on the root.

<?php

$loader = require __DIR__ . '/vendor/autoload.php';

$App = yuxblank\phackp\core\Application::getInstance();

 // get the Application container

$App->bootstrap(__DIR__, '/path of config folder');
 // set the root dir (path of config must be relative after __DIR__

$App->registerService(\yuxblank\phackp\services\ErrorHandlerProvider::class); 
// register pHackp error handler if you want

$App->run(); // fun starts! 

Create configuration folder

All the .php files here will be parsed on bootstrap. Each file must return an associative array. (you can made one single file or many as you need). Create a folder called "config" placed at the same level of the index.php file. If you name the config folder differently, make sure to pass the folder name Application bootstrap method.

Example config.php

<?php
 return [
     'app.globals' => [
         "APP_NAME" => "phackp-bone",
         "APP_VERSION" => "0.1.6-snapshot",
         "AUTHOR" =>
             [
                 "NAME" => "Name",
                 "EMAIL" => "email@devexample.com"
             ],
         "APP_MODE" => "DEBUG",
         'APP_URL' => 'http://localhost:9000',
     ],
 
     "app.http" => [
         "GZIP" => false,
     ],
 
     "app.session" => [
         'LIFETIME' => 1024,
         'USE_COOKIES' => true,
         'NAME' => 'pHackp-session',
         'COOKIE' =>
             [
                 'PATH' => '/',
                 'DOMAIN' => $_SERVER['HTTP_HOST'],
                 'SECURE' => array_key_exists('HTTPS', $_SERVER),
                 'HTTP_ONLY' => false
             ]
 
     ],
 
     'app.view' =>
         [
             'ROOT' => 'src/view',
             'HOOKS' =>
                 [
                     'FOOTER' => 'tags/footer.php',
                     'NAVBAR' => 'tags/navbar.php'
                 ]
         ]
 ];

Make routes file

Example routes.php

<?php
return [
    'routes' => [
        'GET' => [
            [
                'url' => '/',
                'method' => 'index',
                'class' => \controller\App::class,
                'alias' => 'home'
            ], 
            [
                'url' => '/page/{id}',
                'method' => 'page',
                'class' => \controller\App::class,
                'alias' => 'page'
            ], 
        ],

        'POST' => [
        ],
        'PUT' => [
        ],
        'PATCH' => [
        ],
        'DELETE' => [
        ],
        'HEAD' => [
        ],
        'OPTIONS' => [
        ],
        'ERROR' => [
            404 =>
                [
                    'url' => '404',
                    'method' => 'todoNotFound',
                    'class' => Todo::class
                ],
            500 =>
                [
                    'url' => 'error',
                    'method' => 'toDoOnException',
                    'class' => Todo::class
                ],
        ]
    ]
];

Make database file

Example database.php

<?php
return [
    "database" =>
    [
        "ID"      => "default",
        "DSN"     => "mysql:host=localhost;dbname=testdb"
        "USER"    => "root",
        "PSW"     => "",
        "OPTIONS" => [],
    ]
];

Make DI Container definitions file

Example DI.php

<?php
return [
    
    /** example. MyClass is a class that need a configuration
     to be injected by container */
    MyClass::class => function(ContainerInterface $c) {
           return new MyClass($c->get('app.myConfig'));
    }
];

pHackp uses PHP-DI container. For more about definitions: http://php-di.org/doc/php-definitions.html

Register your classes for PSR-4 composer autoloader.

Don't forget to register your namespaces and folders to composer.json in order to provide autoload.

For example:

{
  "name": "yuxblank/phackp-bone",
  "description": "App skeleton for phackp framework applications",
  "license": "MIT",
  "version" : "0.2-alpha",
  "minimum-stability": "dev",
  "authors": [
    {
      "name": "Jhon Doe",
      "email": "example@dev.com"
    }
  ],
  "require": {
    "yuxblank/phackp": "dev-master"
  },
  "autoload": {
    "psr-4": {
      "controller\\": "src/controller",
      "model\\": "src/model"
    }
  }
}

####That's all.

Create the first controller (https://github.com/yuxblank/phackp/wiki/Tutorial-Controllers)

Run the server and go to the url (http://localhost:9000 for instance)*.

\controller\App::class->index() will be serverd.

*Remember to match server url with APP_URL param.