Commit ad74207e authored by nahakyuu's avatar nahakyuu

add yii3 framework to web

parent 8485df9c
This diff is collapsed.
<?php
declare(strict_types=1);
use Psr\SimpleCache\CacheInterface;
use Yiisoft\Cache\ArrayCache;
return [
CacheInterface::class => ArrayCache::class
];
\ No newline at end of file
......@@ -8,4 +8,16 @@ return [
'locale' => 'zh',
'name' => 'phpdts',
],
'locale' => [
'locales' => ['en' => 'en-US', 'zh' => 'zh-CN']
],
'yiisoft/aliases' => [
'aliases' => [
'@root' => dirname(__DIR__, 2),
'@assets' => '@root/public/assets',
'@public' => '@root/public',
'@runtime' => '@root/runtime',
'@vendor' => '@root/vendor',
],
],
];
......@@ -19,6 +19,18 @@ return [
],
'events-console' => '$events',
'bootstrap-console' => '$bootstrap',
'params-web' => [
'$params',
'web/params.php',
],
'di-web' => [
'$di',
'web/di/*.php',
],
'events-web' => '$events',
'routes' => 'web/routes.php',
'bootstrap-web' => '$bootstrap',
],
'config-plugin-environments' => [
'dev' => [
......
<?php
declare(strict_types=1);
use HttpSoft\Message\RequestFactory;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\ServerRequestFactory;
use HttpSoft\Message\StreamFactory;
use HttpSoft\Message\UploadedFileFactory;
use HttpSoft\Message\UriFactory;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
return [
RequestFactoryInterface::class => RequestFactory::class,
ServerRequestFactoryInterface::class => ServerRequestFactory::class,
ResponseFactoryInterface::class => ResponseFactory::class,
StreamFactoryInterface::class => StreamFactory::class,
UriFactoryInterface::class => UriFactory::class,
UploadedFileFactoryInterface::class => UploadedFileFactory::class,
];
<?php
use Yiisoft\Definitions\DynamicReference;
use Yiisoft\Definitions\Reference;
use Yiisoft\Definitions\ReferencesArray;
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
use Yiisoft\Yii\Http\Handler\NotFoundHandler;
use Yiisoft\Session\SessionMiddleware;
use Yiisoft\Csrf\CsrfMiddleware;
use Yiisoft\Router\Middleware\Router;
return [
Yiisoft\Yii\Http\Application::class => [
'__construct()' => [
'dispatcher' => DynamicReference::to([
'class' => MiddlewareDispatcher::class,
'withMiddlewares()' => [
[
SessionMiddleware::class,
CsrfMiddleware::class,
Router::class,
]
],
]),
'fallbackHandler' => Reference::to(NotFoundHandler::class),
],
]
];
<?php
use Yiisoft\Router\Group;
use Yiisoft\Router\RouteCollection;
use Yiisoft\Router\RouteCollectionInterface;
use Yiisoft\Router\RouteCollectorInterface;
return [
RouteCollectionInterface::class => static function (RouteCollectorInterface $collector) use ($config) {
$collector->addGroup(Group::create()->routes(...$config->get('routes')));;
return new RouteCollection($collector);
}
];
<?php
declare(strict_types=1);
return [
];
<?php
declare(strict_types=1);
use Yiisoft\Router\Group;
use Yiisoft\Router\Route;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use NMForce\PHPDTS\Controller\HomeController;
return [
Group::create()
->middleware(FormatDataResponseAsJson::class)
->routes(
Route::get('[/]')
->action([HomeController::class, 'index'])
->name('default'),
Route::get('/home')
->action([HomeController::class, 'index'])
->name('home'),
),
];
assets
\ No newline at end of file
<?php
declare(strict_types=1);
use Yiisoft\Yii\Runner\Http\HttpApplicationRunner;
/**
* @psalm-var string $_SERVER['REQUEST_URI']
*/
// PHP built-in server routing.
if (PHP_SAPI === 'cli-server') {
// Serve static files as is.
/** @psalm-suppress MixedArgument */
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (is_file(__DIR__ . $path)) {
return false;
}
// Explicitly set for URLs with dot.
$_SERVER['SCRIPT_NAME'] = '/index.php';
}
require_once dirname(__DIR__) . '/autoload.php';
// Run HTTP application runner
$runner = new HttpApplicationRunner(
rootPath: dirname(__DIR__),
debug: $_ENV['YII_DEBUG'],
checkEvents: $_ENV['YII_DEBUG'],
environment: $_ENV['YII_ENV'],
);
$runner->run();
<?php
declare(strict_types=1);
namespace NMForce\PHPDTS\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
final class HomeController
{
public function __construct(
private StreamFactoryInterface $streamFactory,
private DataResponseFactoryInterface $responseFactory,
) {
}
public function index(): ResponseInterface
{
return $this->responseFactory->createResponse([
'message' => 'hello',
]);
}
}
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