hkulekci/basicmvc

适用于框架的基本MVC

v0.1.6 2015-09-22 13:18 UTC

This package is auto-updated.

Last update: 2024-09-07 12:40:57 UTC


README

适用于Slim框架的基本MVC使用示例

以下是一个示例目录结构。您可以更改您的结构。您将在配置中指定您的目录。

    app/
        public/
            controller/
                ...
            model/
                ...
            view/
                ...
    .htaccess
    index.php

index.php 文件内容如下。定义您的目录常量,首先初始化Slim,然后初始化BasicMVC。这样就完成了。

<?php 
/************** Some Configurations ****************/
define("APP_DIR", __DIR__ . "/app/");
define("APP_THEME", "default");
define("APP_TEMPLATE", APP_DIR . "public/view/theme/".APP_THEME."/");
/********** End of Some Configurations *************/


require_once __DIR__ . '/../vendor/autoload.php';

use BasicMVC\BasicMVC;

$twigView = new \Slim\Views\Twig();

$app = new \Slim\Slim(
    array(
        'mode'              => 'development', // development, test, and production
        'debug'             => true,
        'view'              => $twigView,
        'templates.path'    => APP_TEMPLATE
        )
    );

$basicmvc = new BasicMVC($app, array(
    "controllers_path"   => APP_DIR . "public/controller/",
    "models_path"        => APP_DIR . "public/model/",
    "library_path"       => APP_DIR . "public/system/library/",
    "template_constants" => array(
        "APP_THEME"     => APP_THEME
    )
));

$app->map('/(:directory(/:controller(/:method(/:args+))))', 
            function (
                $directory = "home", 
                $controller = "home", 
                $method = "index", 
                $args = array()
            ) use($app, $basicmvc) {

    $route = array(
        "directory"       => $directory,
        "controller"      => $controller,
        "method"          => $method,
        );

    echo $basicmvc->run($route, $args);

})->via('GET', 'POST', 'PUT', 'DELETE');

$app->run();