settermjd / zend-expressive-static-pages
An easy, almost painless, way to render static pages in Zend Expressive applications.
Fund package maintenance!
Community Bridge
Requires
- php: ^7.4 || ~8.0.0 || ~8.1.0 || ~8.2.0
- laminas/laminas-config-aggregator: ^1.6.0
- laminas/laminas-diactoros: ^2.8.0
- laminas/laminas-stdlib: ^3.6.0
- mezzio/mezzio: ^3.6.0
Requires (Dev)
- filp/whoops: ^2.5.0
- laminas/laminas-development-mode: ^3.3.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^2.9.2
- zendframework/zend-coding-standard: ^1.0
Suggests
- laminas/laminas-component-installer: ^2.5 for auto-enabling modules
This package is auto-updated.
Last update: 2023-05-10 16:10:06 UTC
README
An, almost, painless way to render static pages in Mezzio applications.
Note: This module does not support laminas-mvc applications.
The intent of this package is to avoid the necessity to create handlers and handler factories just to render static content. It was motivated by various projects that I've worked on, where that seemed to be the case, at least at the time. That approach never made sense to me, so that's that motivated me to scratch my own itch.
Getting Started
To install the package, run composer require settermjd/laminas-static-pages
.
If you want to automate the enabling of the module when running composer require/install/update
, then your project needs to use laminas/laminas-component-installer.
If it does, when the package is installed you'll be asked if you want to enable its ConfigProvider.
Answer with Y
and the package will be ready to use.
If you don't use laminas-component-installer
, or for some reason or other can't, then ensure that \StaticPages\ConfigProvider::class,
is in the ConfigAggregator
list in config/config.php
, as in the example below.
$aggregator = new ConfigAggregator([ \StaticPages\ConfigProvider::class, ]);
With the package installed, you now need to do two further steps:
- Configure the template path
- Create routes
- Create template files
Configure The Template Path
To configure the template path, ensure that in your template paths list, there's one with the key static-pages
, as in the example below.
public function getTemplates() : array { return [ 'paths' => [ 'static-pages' => [__DIR__ . '/../templates/static-pages'], ], ]; }
Create Routes
To create a route for a static page, in your routing table, add one or more named routes where:
- The route’s handler is
StaticPagesHandler::class
- The name follows the convention:
static.<template_file_name_minus_file_extension>
.
Let's assume that we are adding a route for a privacy page and that the template file which will be rendered is privacy.phtml
.
In that case we'd add the following to config/routes.php
:
$app->get('/privacy', StaticPagesHandler::class, 'static.privacy');
Create Template Files
The file can contain whatever you like, it doesn't matter.
That’s It
All being well, this should be all that you need to rapidly serve static content files in your Mezzio applications.