Created
March 3, 2022 13:40
-
-
Save mdmunir/a790bc773ee72f33442768b8d3d83157 to your computer and use it in GitHub Desktop.
Yii di slim fw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Makasih banyak om