PHP code example of forticas / minimal-php-skeleton-mvc

1. Go to this page and download the library: Download forticas/minimal-php-skeleton-mvc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

forticas / minimal-php-skeleton-mvc example snippets


// project-name/src/controller/DefaultController.php


declare(strict_types=1);

namespace App\controller;

use App\core\Controller;

class DefaultController extends Controller
{
    public function my_first_action(){
        //...
        $message = 'Hello World!';
        $user = 'John Doe';
        $this->renderView('defaut_folder_name/file_name',[
            'message' => $message,
            'userName' => $user
            //...
        ]);
    } 
    public function my_second_action(mixed ...$values){
        
    }
}

$this->renderView(string $path, array $args = [], bool $isWithoutLayout = false)

$this->redirectTo(string $path)

$this->redirectToRoute(string $path)

$this->json(array $content)

// project-name/public/index.php

...
$router = new Router();
$router->register('/', '\App\controller\DefaultController::index');
$router->register('/contact', '\App\controller\DefaultController::contact');
$router->register('/posts', '\App\controller\PostController::list');
$router->register('/posts/#id', '\App\controller\PostController::showOne');
$router->register('/posts/#status/orderBy/#date', '\App\controller\PostController::showWithStatusAndOrderBy');
$router->run();

// project-name/src/model/Post.php



declare(strict_types=1);

namespace App\model;

use App\core\Model;

class Post extends Model
{
    private int $id;
    private string $title;
    //...
    
    /**
     * @return int
     */
     public  function getId(): int{
        return $this->id;
    }
    /**
     * @return string
     */
     public  function getTitle(): string{
        return $this->title;
    }
    /**
     * @param string $title
     * @return Post
     */
     public  function setTitle(string $title):self {
        $this->title = $title;
        return $this;
    }
}

project-name
└───core
│   │   Controller.php
│   │   Dao.php
│   │   Model.php
│   │   Router.php
│   
└───public
│   │   index.php
│   │   .htaccess
│   
└───src
│   └───controller
│   └───model
│   └───view
│   │   └───...
│   │   │   layout.php
│   
│   .gitignore
│   .htaccess
│   README.md
│   composer.json
│   config.ini

project-name 
└───...
│
└───src
│   └───...
│   └───view
│   │   └───default
│   │   │   │   index.php
│   │   │   │   contact.php
│   │   └───post
│   │   │   │   list.php
│   │   │   │   show_one.php
│   │   │   layout.php
│   
│   ...