Download the PHP package tobento/service-view without Composer
On this page you can find all versions of the php package tobento/service-view. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package service-view
View Service
The View Service provides a simple yet flexible way to create and render views.
It brings together rendering, shared data, and asset management into a unified and extensible system, making it easy to build structured and maintainable templates.
Table of Contents
- Getting started
- Requirements
- Highlights
- Simple Example
- Documentation
- Data
- Assets
- Assets Handler
- Renderer
- PHP Renderer
- Chain Renderer
- View
- Usage
- Template
- Macros
- Tags Attributes Macro
- Credits
Getting started
Add the latest version of the view service project running this command.
Requirements
- PHP 8.4 or greater
Highlights
- Framework-agnostic, will work with any project
- Decoupled design
- Easy to extend
Simple Example
Here is a simple example of how to use the View service.
We will assume the following directory structure:
Create and render a view
The view template
Documentation
Data
The Data class provides a flexible way to manage variables passed to your views.
You can set global data available to all views, or target specific views individually.
Data can be added, renamed, retrieved, or merged, giving you full control over what each view receives.
Assets
The Assets class provides a convenient way to register, organize, and render assets such as CSS, JavaScript, or any other file-based resources your application needs.
It acts as a lightweight collection where each asset can define its own file path, directory, URI, attributes, group, and render order.
You can also attach an AssetsHandlerInterface implementation to enable advanced features like minification, combining, or rewriting imports.
By default, assets are only collected and rendered as placeholders until you flush them.
Assets Handler
The AssetsHandler provides a flexible way to process and build assets without modifying any view files.
It supports:
- automatic minification (CSS/JS) using the Minify Service
- versioning
- recursive JS import resolution
- circular-import protection
- writing processed assets into a build directory
- replacing asset references
This enables a streamlined build process and ensures that optimized, versioned assets are used consistently throughout your views.
Requirements
The AssetsHandler requires the Minify Service:
Why this is useful
- Asset filenames can change (e.g., through versioning) without requiring updates to views
- Versioning provides reliable cache-busting, ensuring browsers always load the latest assets
- JS imports are resolved recursively, simplifying script organization
- Themes or applications can override assets cleanly
- Production builds are automatically optimized
- The build directory remains consistent and predictable
Notes
- This system is not a bundler (like Vite, Rollup, or Webpack).
- It performs minimal ES module resolution for relative
.jsfiles only. - Non-JS imports are ignored by design.
- Versioning can be disabled via:
->versioning(false)
Example
All methods return a new instance, allowing fluent configuration:
Note
Imported JS files are only re-processed when the root asset is defined in the view.
For example:If the root file is not referenced in the view, changes inside imported files will not trigger an update in the build output unless those imported files are also defined as separate assets.
JavaScript import handling
The asset handler supports rewriting relative JavaScript imports for cache-busting.
Only .js files are processed. CSS and other asset types are not rewritten.
Supported JavaScript import patterns
The following ES module import schemes are recognized and rewritten with versioning:
| Pattern type | Example | Supported |
|---|---|---|
| Same-directory relative import | import a from './foo.js' |
yes |
| Parent-directory import | import b from '../bar.js' |
yes |
| Complex relative paths | import c from './../baz.js' |
yes |
| Deep relative paths | import d from '../../utils/qux.js' |
yes |
Normalized paths (./, ../) |
import e from './nested/../weird/path.js' |
yes |
| Circular imports | a.js → b.js → a.js |
yes |
| Duplicate imports | import x from './foo.js' twice |
yes (deduped) |
All resolved imports are:
- recursively processed
- minified
- written to the build directory
- rewritten with the same version query (for example:
?v=123456)
Unsupported import patterns
The handler intentionally does not process or rewrite the following:
| Pattern type | Example | Reason |
|---|---|---|
| CSS imports | import './styles.css' |
CSS is not rewritten or versioned |
| JSON imports | import data from './data.json' |
Not part of the JS pipeline |
| Bare module imports | import React from 'react' |
Cannot resolve without node_modules |
| Dynamic imports | import('./foo.js') |
Not matched by the static import detection |
| Extensionless imports | import x from './foo' |
Resolver requires a .js extension |
| Directory imports | import utils from './utils' |
No index.js resolution is performed |
These imports remain unchanged in the output.
JavaScript combining
JavaScript combining is supported, but it is not equivalent to bundling.
When multiple JS files are combined, the handler simply concatenates their contents in the order provided.
Because of this:
- JavaScript files are not automatically reordered
importstatements are not moved to the top- no module graph or dependency ordering is performed
Combining JavaScript is therefore only suitable when the files are already structured in a way that allows safe concatenation.
Recommendation:
Use JavaScript combining only for files that do not rely on ES module ordering, or when using a modifier that explicitly supports import rewriting. Without such a modifier, JS files are processed individually, and combining is primarily intended for CSS assets.
Renderer
PHP Renderer
The PHP renderer uses native PHP as its templating language, so there is no new syntax to learn.
It also integrates with the Dir Service, which provides a simple and flexible way to organize and prioritize your template directories.
Chain Renderer
The chain renderer allows you to combine multiple renderers into a single rendering pipeline.
If the first renderer cannot handle a view, the next renderer in the chain is tried, and so on.
This makes it easy to mix different rendering engines (e.g., PHP, Twig) in the same application.
View
Usage
The View class ties everything together: a renderer, shared data, and asset management.
It provides a simple API for rendering templates, passing data, registering view aliases, and hooking into the rendering process with event callbacks.
Auto Rendering Views
You may automatically render a view before or after another view is rendered.
This is useful for injecting reusable UI fragments (meta tags, banners, notifications, tracking scripts) without modifying templates.
View Callbacks (on)
You may hook into the rendering process of a specific view and modify its data before it is rendered.
This is useful for preparing dynamic data, registering view aliases, or conditionally preventing rendering.
Wildcard Example
Example with key and add()
This example registers an alias so that rendering comments.writing will load the comments/writing view.
Template
A template is simply a PHP file rendered by the view system.
You can access the $view instance inside any template, allowing you to escape values, include subviews, register assets, or conditionally render content.
Macros
Macros allow you to extend the View class with your own custom helper methods.
This makes it easy to add reusable functionality directly on the view instance without creating subclasses.
Tags Attributes Macro
The Tags Attributes macro provides a convenient way to collect and manage HTML attributes for specific tags.
By registering the macro on the view instance, you can easily retrieve an AttributesInterface object and work with attributes in a structured way.
Check out the Tag Service - Attributes Interface to learn more about how attributes work in general.
Credits
- Tobias Strub
- All Contributors
All versions of service-view with dependencies
psr/log Version ^3.0
tobento/service-macro Version ^2.0
tobento/service-filesystem Version ^2.0
tobento/service-dir Version ^2.0
tobento/service-support Version ^2.0
tobento/service-tag Version ^2.0