View utilities for your PHP web app.
I created this library to be able to pull it in an existing web app without too much effort setting it up.
Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.
- folded/action: A way to organize your controllers for your web app.
- folded/config: Configuration utilities for your PHP web app.
- folded/crypt: Encrypt and decrypt strings for your web app.
- folded/exception: Various kind of exception to throw for your web app.
- folded/history: Manipulate the browser history for your web app.
- folded/http: HTTP utilities for your web app.
- folded/orm: An ORM for you web app.
- folded/request: Request utilities, including a request validator, for your PHP web app.
- folded/routing: Routing functions for your PHP web app.
- folded/session: Session functions for your web app.
- Can render Blade views, and pass data to it
- Can render plain PHP views
- Can add data to a specific views beforehand (handy if you always need some data in your layouts for example)
- Eager load the view engine such as it is not booted until you call
displayView()
- PHP version >= 7.4.0
- Composer installed
In your root folder directory, run this command:
composer require folded/view
In the script that is called before displaying your view, add this set up code:
use function Folded\setViewFolderPath;
use function Folded\setViewCacheFolderPath;
setViewFolderPath(__DIR__ . "/views");
setViewCacheFolderPath(__DIR__ . "/cache/views");
The cache is a place where your code is compiled to plain PHP, and stored in the disk, for faster rendering for next requests displaying the view.
Since this library relies on Laravel's illuminate/view, you can refer to the 7.X documentation if you need any information regarding the Blade syntax and the available Blade directives.
- 1. Display a view
- 2. Pass data to your view
- 3. Display a plain PHP view
- 4. Always pass certain data to a view
- 5. Get the rendered view
In this example, we will display a Blade view.
use function Folded\displayView;
displayView("home.index");
With the following content inside views/home/index.blade.php
:
<h1>Hello world</h1>
In this example, we will pass a string to the view we display.
use function Folded\displayView;
displayView("home.index", [
"name" => "John",
]);
With the following content inside the view:
<h1>Hello world</h1>
<p>Welcome to my website, {{ $name }}.</p>
In this example, we will display a plain PHP file.
use function Folded\displayView;
displayView("about-us.index");
The plain PHP view is located at views/about-us/index.php
(notice there is no "blade" extension now), with the following content:
<h1>About us</h1>
In this example, we will pass a company name to a layout view, to be able to not add it to every view that extends the layout.
use function Folded\addDataToView;
addDataToView("layouts.base", [
"companyName" => "Folded",
]);
In this example, we will get the rendered view in a variable (useful to send emails for example).
use function Folded\getRenderedView;
$content = getRenderedView("emails.account-created");
7.3 | 7.4 | 8.0 | |
---|---|---|---|
v0.1.0 | ❌ | ✔️ | ❓ |
v0.1.1 | ❌ | ✔️ | ❓ |
v0.1.2 | ❌ | ✔️ | ❓ |
v0.2.0 | ❌ | ✔️ | ❓ |
v0.2.1 | ❌ | ✔️ | ❓ |
v0.2.2 | ❌ | ✔️ | ❓ |
v0.2.3 | ❌ | ✔️ | ❓ |
v0.3.0 | ❌ | ✔️ | ❓ |