Commit d9209f90 authored by Nemo Ma's avatar Nemo Ma Committed by GitHub

Merge pull request #112 from nahakyuu/yii3_frameword_demo

增加了使用dz模板的demo
parents ec89e019 c3995298
......@@ -35,4 +35,4 @@ bash ./bot/bot_enable.sh
4. 目前只加入了`src``config`文件夹,demo文件见`C:\git\phpdts\src\Controller\HomeController.php`
5. 可以使用[Yii Dev Panel](https://yiisoft.github.io/yii-dev-panel)来调试
6. 数据库配置请修改 `config\common\params.php`,默认使用`Yiisoft\Db\Mysql\ConnectionPDO`
7. 目前只用了`yii serve`使用php原生自带的Routing file功能做路由,nginx和`.htaccess`在做了在做了
\ No newline at end of file
7. 目前只用了`yii serve -t .``-t`参数是必要的,用来指定docroot,不然访问不到静态文件,使用php原生自带的Routing file功能做路由,nginx和`.htaccess`在做了在做了
\ No newline at end of file
This diff is collapsed.
......@@ -2,20 +2,36 @@
declare(strict_types=1);
use yiisoft\Definitions\DynamicReference;
use yiisoft\Definitions\Reference;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Mysql\ConnectionPDO;
use Yiisoft\Db\Driver\PDO\PDODriverInterface;
use Yiisoft\Db\Mysql\PDODriver;
use Yiisoft\Db\Mysql\Dsn;
/** @var array $params */
return [
ConnectionInterface::class => ConnectionPDO::class,
PDODriverInterface::class => PDODriver::class,
PDODriver::class => [
'__construct()' => [
'dsn' => $params['db']['dsn'],
'dsn' => DynamicReference::to(static fn (Dsn $dsn) => $dsn->asString()),
'username' => $params['db']['username'],
'password' => $params['db']['password'],
]
],
Dsn::class => [
'__construct()' => [
'driver' => $params['db']['driver'],
'host' => $params['db']['host'],
'databaseName' => $params['db']['databaseName'],
'port' => $params['db']['port'],
'options' => $params['db']['options']
]
]
];
......@@ -2,18 +2,6 @@
declare(strict_types=1);
use Yiisoft\Db\Mysql\Dsn;
$db = [
'driver' => 'mysql',
'host' => 'localhost',
'databaseName' => 'acdts3',
'port' => '3306',
'options' => ['charset' => 'utf8mb4'],
'username' => 'root',
'password' => 'mylittlepony',
];
return [
'app' => [
'charset' => 'UTF-8',
......@@ -26,19 +14,17 @@ return [
'yiisoft/aliases' => [
'aliases' => [
'@root' => dirname(__DIR__, 2),
'@assets' => '@root/public/assets',
'@public' => '@root/public',
'@runtime' => '@root/runtime',
'@vendor' => '@root/vendor',
],
],
'db' => array_merge($db, [
'dsn' => (new Dsn(
$db['driver'],
$db['host'],
$db['databaseName'],
$db['port'],
$db['options']
))->asString(),
]),
'db' => [
'driver' => 'mysql',
'host' => 'localhost',
'databaseName' => 'acdts3',
'port' => '3306',
'options' => ['charset' => 'utf8mb4'],
'username' => 'root',
'password' => 'mylittlepony',
]
];
......@@ -7,6 +7,7 @@ use Yiisoft\Definitions\Reference;
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
use Yiisoft\Yii\Http\Handler\NotFoundHandler;
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
use Yiisoft\Session\SessionMiddleware;
use Yiisoft\Csrf\CsrfMiddleware;
use Yiisoft\Router\Middleware\Router;
......@@ -18,6 +19,7 @@ return [
'class' => MiddlewareDispatcher::class,
'withMiddlewares()' => [
[
ErrorCatcher::class,
SessionMiddleware::class,
CsrfMiddleware::class,
Router::class,
......
<?php
declare(strict_types=1);
use Yiisoft\Router\Group;
use Yiisoft\Router\RouteCollection;
use Yiisoft\Router\RouteCollectionInterface;
......
<?php
declare(strict_types=1);
use Yiisoft\Definitions\DynamicReference;
use Yiisoft\Definitions\Reference;
use Yiisoft\Aliases\Aliases;
use Yiisoft\View\WebView;
use NMForce\PHPDTS\View\DiscuzTemplateRenderer;
/** @var array $params */
return [
WebView::class => [
'__construct()' => [
'basePath' => DynamicReference::to(
static fn (Aliases $aliases) => $aliases->get($params['yiisoft/view']['basePath'])
),
],
'withDefaultExtension()' => [
'htm',
],
'withRenderers()' => [['htm' => Reference::to(DiscuzTemplateRenderer::class)]],
'setParameters()' => [
$params['yiisoft/view']['parameters'],
],
'reset' => function () use ($params) {
/** @var WebView $this */
$this->clear();
$this->setParameters($params['yiisoft/view']['parameters']);
},
],
];
......@@ -2,5 +2,23 @@
declare(strict_types=1);
use Yiisoft\Definitions\Reference;
use NMForce\PHPDTS\ViewInjection\ApplicationViewInjection;
return [
'yiisoft/aliases' => [
'aliases' => [
'@public' => '@root/public',
'@assets' => '@public/assets',
'@layout' => '@root/templates/default',
'@views' => '@root/templates/default',
],
],
'yiisoft/yii-view' => [
'layout' => null,
'injections' => [
Reference::to(ApplicationViewInjection::class),
],
],
];
......@@ -6,18 +6,16 @@ use Yiisoft\Router\Group;
use Yiisoft\Router\Route;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsHtml;
use NMForce\PHPDTS\Controller\HomeController;
return [
Group::create()
->middleware(FormatDataResponseAsJson::class)
->middleware(FormatDataResponseAsHtml::class)
->routes(
Route::get('[/]')
->action([HomeController::class, 'index'])
->name('default'),
Route::get('/home')
->action([HomeController::class, 'index'])
->name('home'),
->name('/acdts/index'),
),
];
......@@ -16,6 +16,9 @@ if (PHP_SAPI === 'cli-server') {
return false;
}
if (is_file(dirname(__DIR__) . $path)) {
return false;
}
// Explicitly set for URLs with dot.
$_SERVER['SCRIPT_NAME'] = '/index.php';
}
......
......@@ -5,22 +5,29 @@ declare(strict_types=1);
namespace NMForce\PHPDTS\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
use Yiisoft\Yii\View\ViewRenderer;
final class HomeController
{
public function __construct(
private StreamFactoryInterface $streamFactory,
private DataResponseFactoryInterface $responseFactory,
private ViewRenderer $viewRenderer,
) {
}
public function index(): ResponseInterface
{
return $this->responseFactory->createResponse([
'message' => 'hello',
$now = time() + 8 * 3600 + 0 * 60;
list($sec, $min, $hour, $day, $month, $year, $wday) = explode(',', date("s,i,H,j,n,Y,w", $now));
return $this->viewRenderer->render('index', [
'sec' => $sec,
'min' => $min,
'hour' => $hour,
'day' => $day,
'month' => $month,
'year' => $year,
'wday' => $wday,
'now' => $now,
]);
}
}
<?php
declare(strict_types=1);
namespace NMForce\PHPDTS\View;
use Throwable;
use function extract;
use function func_get_arg;
use function ob_end_clean;
use function ob_get_clean;
use function ob_get_level;
use function ob_implicit_flush;
use function ob_start;
use Yiisoft\Aliases\Aliases;
use Yiisoft\View\ViewInterface;
use Yiisoft\View\TemplateRendererInterface;
final class DiscuzTemplateRenderer implements TemplateRendererInterface
{
public function __construct(private Aliases $aliases)
{
}
public function render(ViewInterface $view, string $template, array $parameters): string
{
defined('IN_GAME') or define('IN_GAME', TRUE);
defined('GAME_ROOT') or define('GAME_ROOT', "{$this->aliases->get('@root')}/");
defined('TPLDIR') or define('TPLDIR', './templates/default');
defined('TEMPLATEID') or define('TEMPLATEID', 1);
defined('CURSCRIPT') or define('CURSCRIPT', 'index');
$tplrefresh = 1;
require_once GAME_ROOT . './include/global.func.php';
$renderer = function (): void {
extract(func_get_arg(1), EXTR_OVERWRITE);
require template(basename(func_get_arg(0), '.htm'));
};
$obInitialLevel = ob_get_level();
ob_start();
ob_implicit_flush(false);
try {
$renderer->bindTo($view)($template, $parameters);
return ob_get_clean();
} catch (Throwable $e) {
while (ob_get_level() > $obInitialLevel) {
ob_end_clean();
}
throw $e;
}
}
}
<?php
declare(strict_types=1);
namespace NMForce\PHPDTS\ViewInjection;
use Yiisoft\Yii\View\CommonParametersInjectionInterface;
use Yiisoft\Aliases\Aliases;
final class ApplicationViewInjection implements CommonParametersInjectionInterface
{
public function __construct(
private Aliases $aliases
) {
}
public function getCommonParameters(): array
{
defined('IN_GAME') or define('IN_GAME', TRUE);
defined('GAME_ROOT') or define('GAME_ROOT', "{$this->aliases->get('@root')}/");
require GAME_ROOT . './include/global.func.php';
$vars = get_defined_vars();
require GAME_ROOT . './config.inc.php';
require GAME_ROOT . './gamedata/system.php';
require GAME_ROOT . './gamedata/gameinfo.php';
require GAME_ROOT . './gamedata/combatinfo.php';
$adminmsg = file_get_contents(GAME_ROOT . './gamedata/adminmsg.htm');
$systemmsg = file_get_contents(GAME_ROOT . './gamedata/systemmsg.htm');
$cuser = '苹果';
$error = '';
require config('resources');
require config('gamecfg');
$vars = array_diff(get_defined_vars(), $vars);
return $vars;
// return [
// // 'charset' => $this->charset,
// // 'allowcsscache' => 1,
// // 'homepage' => 'http://www.amarilloviridian.com/',
// // 'gameversion'=> 'GE942 ~TORONTO',
// ];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment