janst123/jsmf

一个用于构建基于MVC应用程序的小型PHP框架

v1.0.6 2019-06-24 06:17 UTC

This package is auto-updated.

Last update: 2024-09-24 17:29:51 UTC


README

使用此框架,您可以构建一个完整的基于MVC(模型-视图-控制器)的PHP应用程序,或者仅将其用作一组有用的PHP类的集合。框架的所有部分都可以单独使用。

查看ApiIndex,了解所有可用的类和方法。

在文档处于不完整状态时,请参阅示例应用程序

安装

您可以通过Composer安装JSMF。将以下依赖项添加到您的composer.json中

{
  "require": {
    "janst123/jsmf":">=1.0.0"
}

您也可以从本存储库克隆JSMF(使用版本标签或克隆master分支以获取最新更改)。在这种情况下,您必须编写自己的自动加载器。

示例应用程序引导

如果您希望将整个应用程序基于JSMF,则需要此步骤。您也可以使用单个类,使用JSMF自动加载器或您自己的。

将此代码放在您的应用程序索引文件中。通过此文件路由所有请求(有关如何使用Apache将所有请求路由到index.php的介绍,请参阅Gist)。使用此最小设置将让JSMF\Application类根据请求URL(http://host/module/controller/action)确定模型/控制器/操作。

如果一个或多个URL部分不存在,应用程序将始终使用“index”操作(“index”控制器,“index”模块)。

示例:请求http://host将尝试调用模块“index” -> IndexController -> indexAction,请求到http://host/misc/faq将调用模块“misc” -> FaqController -> indexAction

<?php
define('DEV_SERVER', true); // define this 
define('SRC', realpath(dirname(__FILE__) . '/../'));
require(SRC . '/vendor/autoload.php');

try {
  // optional: load an application wide config
  JSMF\Config::load(SRC . '/config/base.config.php');

  // optional: load application wide translations
  JSMF\Language::loadTranslations(SRC . '/language/translations', 'de');

  // optional: route special URLs to special modules / controllers / actions ( I always place the legal texts in a Module named misc)
  JSMF\Request::addRoute('/^\/disclaimer\/?$/i', 'misc', 'index', 'disclaimer'); // route a request to /disclaimer to the disclaimer Action in the IndexController in the module "misc"
  JSMF\Request::addRoute('/^\/privacy\/?$/i', 'misc', 'index', 'privacy');

  // register the autoloader and run the application
  JSMF\Application::registerAutoloader();
  JSMF\Application::run();

  // output the applications response (can be HTML, JSON ...)
  JSMF\Response::output();

} catch(JSMF\Exception\NotFound $e) {
  // do some special things for not-found errors e.g. redirect to a static 404 page
  JSMF\Request::redirect('/404.html');
  JSMF\Response::output(); // output method also sends the headers (here: the Location header)

} catch(JSMF\Exception $e) {
  // output is done by JSMF
  JSMF\Response::setException($e);
  JSMF\Response::output();

} catch(Exception $e) {
  // do something on common non-JSMF Exception
}