1. Install

SuitUp - PHP Framework

Install by composer

composer require braghim-sistemas/suitup-php

Project structure

This is the recommended structure to the project, remember that inside of each module folder must exist these following folders Controllers, Views and Models which must to follow this structure, Form structure is optional. You can store your assets (resources) where God touch in your heart, I use to put it in the project root in a folder named assets.

By default the first module must to be always named ModuleDefault.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    assets
    |  example.jpg
    config
    |  database.config.php
    modules
    |  ModuleDefault
    |  |  Controllers
    |  |  |  IndexController.php
    |  |  |  ErrorController.php
    |  |  Form
    |  |  |  Index
    |  |  |  |  Index.php
    |  |  Model
    |  |  |  Gateway
    |  |  |  |  User.php
    |  |  |  UserBusiness.php
    |  |  views
    |  |  |  index
    |  |  |  |  index.phtml
    |  |  |  error
    |  |  |  |  not-found.phtml
    |  |  |  |  error.phtml
    |  ModuleAdmin
    .htaccess
    composer.json
    composer.phar
    index.php

.htaccess

Our framework only works with mod rewrite enabled in Apache.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
RewriteEngine on

# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

index.php

This is the main file from the system, every execution in our project will pass by here first.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
session_start();

// Sao Paulo Timezone (Brazil - Choose yours)
date_default_timezone_set('America/Sao_Paulo');

// Constants configs setted in VirtualHost (we will get there)
define('DEVELOPMENT', (bool) getenv("DEVELOPMENT"));
define('SHOW_ERRORS', (bool) getenv("SHOW_ERRORS"));
if (SHOW_ERRORS) {
    error_reporting(E_ALL);
    ini_set('display_errors', true);
}

// Root path
// Everything is related to the root path
chdir(__DIR__);

// Simple functions defined by you as help (optional)
// include_once './functions.php';

// Setup autoloading composer
if (file_exists('vendor/autoload.php')) {
    $loader = include 'vendor/autoload.php';

    // If you want, can add your libraries here with composer
    // $loader->add('System', 'library/.');
} else {
    exit("Project dependencies not found, execute 'php composer.phar install' in the root of project");
}

// To run our framework, we actually must to run just this few lines below
$mvc = new SuitUpStart('relative/path/to/your/modules/');

// If you want, you can monitoring your SQL queries
// just adding this line below
$mvc->setSqlMonitor(DEVELOPMENT);

// Here is where fun begin! \o/
$mvc->run();

Virtual Host

We recommend you to create your own Virtual Host for each application, simulating with that an environment more next to the production application working

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<VirtualHost *:80>
        ServerAdmin youremail@yourhost.com.br

        ServerName yourprojectname.dev
        ServerAlias yourprojectname.dev

        DocumentRoot /var/www/yourProject
        <Directory /var/www/yourProject>
                Options Indexes FollowSymLinks
                AllowOverride All 
                Order allow,deny
                Allow from all 
        </Directory>

        // We don't show these folders created as recommended structure
        // because it just don't mind. You can choose whatever folder to
        // store your log files, feel in home ;)
        ErrorLog /var/www/yourProject/var/log/error.log
        CustomLog /var/www/yourProject/var/log/access.log combined

        LogLevel warn

        # Environment variables
        # Make sure to not include this variables online or even make it always false
        SetEnv DEVELOPMENT true
        SetEnv SHOW_ERRORS true
</VirtualHost>

This is everything you must to know to make it works. Good job, thank you!