Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Created March 3, 2022 13:40
Show Gist options
  • Save mdmunir/a790bc773ee72f33442768b8d3d83157 to your computer and use it in GitHub Desktop.
Save mdmunir/a790bc773ee72f33442768b8d3d83157 to your computer and use it in GitHub Desktop.
Yii di slim fw
<?php
use yii\web\View;
//use yii\helpers\Html;
/* @var $this View */
$js = <<<JS
setInterval(function(){
let d = new Date();
\$('#time').text(d.toString());
}, 1000);
JS;
$this->registerJs($js);
?>
<h1><?= $name ?></h1>
<span id="time"></span>
<?php
use yii\web\View;
use yii\helpers\Html;
/* @var $this View */
?>
<?php $this->beginPage(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?= 'UTF-8' ?>" />
<title><?= Html::encode($this->title); ?></title>
<?php $this->head(); ?>
</head>
<body>
<?php $this->beginBody(); ?>
<?= $content ?>
<?php $this->endBody(); ?>
</body>
</html>
<?php $this->endPage(); ?>
<?php
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
class DView implements yii\base\ViewContextInterface
{
public $layout = '//layouts/main';
public function getViewPath()
{
return Yii::$app->getViewPath();
}
public function render($view, $params = [])
{
$appView = Yii::$app->view;
$content = $appView->render($view, $params, $this);
if ($this->layout) {
return $appView->render($this->layout, ['content' => $content]);
}
return $content;
}
}
$config = [
'id' => 'app',
'basePath' => __DIR__,
'viewPath' => __DIR__ . '/views',
'vendorPath' => __DIR__.'/../vendor',
'aliases' => [
'@app' => __DIR__,
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => '',
'username' => '',
'password' => '',
],
'view' => [
'class' => 'yii\web\View'
],
'request' => [
'class' => 'yii\web\Request',
'enableCsrfValidation' => false,
'enableCookieValidation' => false,
'enableCsrfCookie' => false,
],
'dView' => [
'class' => 'DView'
]
],
];
new yii\web\Application($config);
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../app/yii.php';
$app = AppFactory::create();
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write("Hello world!");
return $response;
});
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
/* @var $dView \DView*/
$dView = Yii::$app->dView;
$content = $dView->render('hello', ['name' => $name]);
$response->getBody()->write($content);
return $response;
});
$app->run();
@huhufajri
Copy link

Makasih banyak om

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment