NB: this is for education purpose
You can create new project via composer:
composer create-project forticas/minimal-php-skeleton-mvc project-name
controller
, model
and view
folders are inside src
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
change configuration inside config.ini
[database]
host= localhost
dbname = your_db_name
;default MySQL port
port = 3306
username = your_db_username
password = your_db_password
[server]
;your project folder name
base_uri = /project-name
// project-name/src/controller/DefaultController.php
<?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)
define routing inside index.php
Examples :
// 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();
Don't delete
layout.php
file.
create your views files inside project-name/src/view/
Examples :
project-name
└───...
│
└───src
│ └───...
│ └───view
│ │ └───default
│ │ │ │ index.php
│ │ │ │ contact.php
│ │ └───post
│ │ │ │ list.php
│ │ │ │ show_one.php
│ │ │ layout.php
│
│ ...
Example :
// project-name/src/model/Post.php
<?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;
}
}
Coming soon...