Selamat Datang di Dapur AdeMS
Dapur AdeMS sudah terpasang dan berjalan dengan baik.
Halaman yang sedang Anda lihat ini adalah halaman sementara. Halaman ini akan otomatis diganti begitu Anda menambahkan route
home
dengan mengikuti langkah-langkah di bawah.
Cara Membuat Halaman Home
Ikuti 9 langkah berikut untuk membuat modul Home. Nama modul boleh Anda ganti sesuai keinginan.
Buat folder modul baru di dalam module/. Pada contoh ini modulnya bernama Home.
module/
├── App/ ← modul bawaan Dapur AdeMS
└── Home/ ← modul baru Anda
├── src/
│ ├── ConfigProvider.php
│ └── Handler/
│ ├── HomePageHandler.php
│ └── HomePageHandlerFactory.php
└── templates/
└── home/
└── home-page.phtml
Handler menerima request dan merender template di dalam layout::default.
<?php
declare(strict_types=1);
namespace Home\Handler;
use Laminas\Diactoros\Response\HtmlResponse;
use Mezzio\Template\TemplateRendererInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
final class HomePageHandler implements RequestHandlerInterface
{
public function __construct(
private readonly TemplateRendererInterface $template
) {
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new HtmlResponse($this->template->render('home::home-page', [
'layout' => 'layout::default',
]));
}
}
Factory menyiapkan dependensi yang dibutuhkan handler.
<?php
declare(strict_types=1);
namespace Home\Handler;
use Mezzio\Template\TemplateRendererInterface;
use Psr\Container\ContainerInterface;
final class HomePageHandlerFactory
{
public function __invoke(ContainerInterface $container): HomePageHandler
{
return new HomePageHandler($container->get(TemplateRendererInterface::class));
}
}
Isi halaman home Anda. Template ini akan otomatis dibungkus oleh layout Dapur AdeMS.
<?php
$this->headTitle('Beranda');
?>
<div class="container">
<div class="jumbotron">
<h1>Halo, ini halaman home saya!</h1>
<p class="lead">Mulai buat website Anda di sini.</p>
</div>
</div>
ConfigProvider mendaftarkan factory handler dan lokasi template modul.
<?php
declare(strict_types=1);
namespace Home;
final class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => [
'factories' => [
Handler\HomePageHandler::class => Handler\HomePageHandlerFactory::class,
],
],
'templates' => [
'paths' => [
'home' => [__DIR__ . '/../templates/home'],
],
],
];
}
}
Tambahkan namespace Home\ di bagian autoload, lalu jalankan composer dump-autoload.
"autoload": {
"psr-4": {
"App\\": "module/App/src/",
"Home\\": "module/Home/src/"
}
},
Tambahkan ConfigProvider modul ke daftar konfigurasi, di bawah App\ConfigProvider.
// Default App module config
App\ConfigProvider::class,
// Modul halaman home
Home\ConfigProvider::class,
// Memuat konfigurasi tambahan dari config/autoload/:
// `global.php`, `*.global.php`, `local.php`, dan `*.local.php`
new PhpFileProvider(realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php'),
], $cacheConfig['config_cache_path']);
Hubungkan alamat / ke handler Anda dengan nama route home.
return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void {
$app->get('/', Home\Handler\HomePageHandler::class, 'home');
};
Konfigurasi Dapur AdeMS disimpan di cache data/cache/config-cache.php. Hapus cache ini agar modul baru dari langkah 7 terbaca. Lakukan juga setiap kali Anda mengubah konfigurasi atau ConfigProvider.
composer clear-config-cache
# atau hapus file berikut secara manual:
# data/cache/config-cache.php