forticas/minimal-php-skeleton-mvc

此项目用于教育目的

1.0.0 2022-02-28 17:29 UTC

This package is auto-updated.

Last update: 2024-09-08 18:34:30 UTC


README

注意: 此项目用于教育目的

安装

您可以通过composer创建新项目

composer create-project forticas/minimal-php-skeleton-mvc project-name

项目结构

controllermodelview 文件夹位于 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

用法

1. 配置

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

2. 控制器

// 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)

3. 路由

index.php 中定义路由

示例

// 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();

4. 视图

不要删除 layout.php 文件。

project-name/src/view/ 内创建您的视图文件

示例

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

4. 模型

示例

// 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;
    }
}

5. 数据库预定义方法

即将推出...