phly / phly-simple-page
ZF2 Module for easily creating static pages
Installs: 21 014
Dependents: 1
Suggesters: 1
Security: 0
Stars: 34
Watchers: 11
Forks: 12
Open Issues: 5
Requires
- php: ^7.2
- laminas/laminas-cache: ^2.9
- laminas/laminas-eventmanager: ^3.2.1
- laminas/laminas-http: ^2.9.1
- laminas/laminas-mvc: ^3.1.1
- laminas/laminas-stdlib: ^3.2.1
- laminas/laminas-view: ^2.9.1
- ocramius/package-versions: ^1.0
- symfony/console: ^4.0 || ^5.0
Requires (Dev)
- laminas/laminas-coding-standard: ~2.0.0@rc || ~2.0.0
- laminas/laminas-servicemanager: ^3.4.0
- phpunit/phpunit: ^8.5.2
This package is auto-updated.
Last update: 2024-12-11 01:03:47 UTC
README
A Laminas MVC module for "static" pages.
Overview
In most Laminas MVC applications, you'll have at least a few pages that are basically static — the controller contains no logic for the given endpoint, and it simply renders a template.
By default, this requires the following steps:
- Create a route
- Create a controller (if you don't have one already)
- Create an action in that controller
- Create a template
This module halves the workflow by eliminating the middle two steps.
Installation
Use Composer:
$ composer require phly/phly-simple-page
Enable the module
If you are using laminas-component-installer,
you will get prompted to add the module to your config/application.config.php
file.
If you are not, or you choose not to use the component installer, you can enable
it by adding manually it to your config/application.config.php
file:
<?php return [ 'modules' => [ 'PhlySimplePage', 'Application', ], ];
Usage
Create configuration in your application, mapping a route to the controller
PhlySimplePage\PageController
, and specifying a template
key in the route
defaults.
use PhlySimplePage\PageController; return [ 'router' => [ 'routes' => [ 'about' => [ 'type' => 'Literal', 'options' => [ 'route' => '/about', 'defaults' => [ 'controller' => PageController::class, 'template' => 'application/pages/about', // optionally set a specific layout for this page 'layout' => 'layout/some-layout', ], ], ], ], ], ];
Then, make sure you create a template for the page. In the above example, I'd
likely create the file in module/Application/view/application/pages/about.phtml
.
Caching
You can enable a write-through cache for all pages served by the
PageController
. This is done via the following steps:
- Creating cache configuration
- Enabling the page cache factory
To create cache configuration, create a phly-simple-page
configuration key in
your configuration, with a cache
subkey, and configuration suitable for
Laminas\Cache\StorageFactory::factory
. As an example, the following would setup
filesystem caching:
return [ 'phly-simple-page' => [ 'cache' => [ 'adapter' => [ 'name' => 'filesystem', 'options' => [ 'namespace' => 'pages', 'cache_dir' => getcwd() . '/data/cache', 'dir_permission' => '0777', 'file_permission' => '0666', ], ], ], ], ];
To enable the page cache factory, do the following:
return [ 'service_manager' => [ 'factories' => [ 'PhlySimplePage\PageCache' => \PhlySimplePage\PageCacheFactory::class, ], ], ];
Selectively disabling caching for given routes
If you do not want to cache a specific page/route, you can disable it by
adding the default key do_not_cache
with a boolean true
value to the route.
As an example:
'about' => [ 'type' => 'Literal', 'options' => [ 'route' => '/about', 'defaults' => [ 'controller' => \PhlySimplePage\PageController::class, 'template' => 'application/pages/about', 'do_not_cache' => true, ], ], ],
Clearing the cache
To clear the cache for any given page, or for all pages, your cache adapter (a) must support cache removal from the command line (APC, ZendServer, and several other adapters do not), and (b) must support flushing if you wish to clear all page caches at once.
The module provides a vendor binary, phly-simple-page
for accomplishing this:
-
./vendor/bin/phly-simple-page clear:cache
will clear all cached pages at once. -
./vendor/bin/phly-simple-page clear:cache --page=
clear a single cached page; use the template name you used in the routing configuration as the page value.
TODO
- Ability to clear sets of pages