3. Inside your application
script-ui serve is for your own computer. It has no login check, so it answers nobody else. Colleagues need the tools on a server, behind the login of your application.
There are two ways to get there. In plain PHP, one script behind your web server calls serve(), and the library sends the page. In Symfony or Laravel, a controller calls respond(), and the framework sends it.
The rest of this page is the text of the README, word for word, so it cannot say something else than the manual does.
A folder of tools behind your web server
A folder of tools. Put this in a script behind your web server:
// public/admin.php: the only file of all this that lies in the web root
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../bootstrap.php'; // starts your session, connects your database
Runio\ScriptUi\serve(
__DIR__ . '/../tools',
auth: fn () => $_SESSION['admin_email'] ?? null, // who is asking, or null to refuse
log: fn (array $entry) => $auditLog->write($entry), // called for every button a user presses and every file a user downloads
name: 'Back office',
);An entry is an array with time, user, tool, action (the label of the button), key, confirmed, inputs, error, field_errors and download. field_errors holds what the page said is wrong in that run, by the key of the input: the messages of field_error(), and those of a file field that turned a file down. When it is not empty, the button most likely did not do its work. inputs holds what every input of the page returned in that run, by key. Not every value is a text: a tick box gives true or false, multiselect() a list of options, a table the ids of its ticked rows, and a file field the name, the size and the SHA-256 of the file. Turn the entry into JSON if your log takes one line of text.
Every PHP file in the folder becomes a tool, listed on an index page. The first comment in a file is its title, more comment lines are its description. Files that start with _ are helpers and are never served.
Keep the folder of tools out of the web root, next to vendor/. Only the script above belongs where the web server looks for files. A file that the web server runs by itself never passes serve(), so nobody asks auth: or allow: about it. That goes for the helper files in the folder too.
When the folder has to lie in the web root, close it in the web server. With Apache 2.4 that is a file .htaccess in the folder with the line Require all denied (it needs AllowOverride). With nginx it is location ^~ /tools/ { deny all; }. Then try it: /tools/refund.php has to answer 403 or 404.
The list of tools says so when the folder lies in the web root. It says it only where error details are shown: on your own computer without a login check, or with debug: true. The library cannot see what the web server was told, so the note stays while the folder lies there. No note is not a proof that the folder is closed. Where the web server runs on another machine or in another container, PHP may not see the web root at all, and a link from the web root to the folder is not seen either.
With a login check, error details go to the error log and not into the page. Pass debug: true where you develop.
Who may open which tool. The login check lets somebody in, and then every tool is open to them. allow: decides for one user and one tool:
Runio\ScriptUi\serve(
__DIR__ . '/../tools',
auth: fn () => $_SESSION['admin_email'] ?? null,
allow: fn (mixed $user, string $tool) => $permissions->has($user, 'tool.' . $tool),
);It gets two things. The first is what your login check returned, as it is: a name, a number or the user object of your application. It is null when the login check only said true, and when there is no login check. The second is the id of the tool: the file name without .php, so refund for tools/refund.php. It answers true or false. Compare strictly where you compare: in_array($user, $team, true).
A tool that is not allowed is left out of the list, and whoever opens its address reads "Not allowed" before anything of the tool runs, whether that is a page view, a pressed button or a download. The question is asked on every request, so access that you take away ends with the next click, also on a page that is open.
Only true opens a tool. Any answer other than true or false, and a check that throws, lets nobody in, and the reason goes to the error log. That holds for the list too: it asks once for every tool, so one answer that is neither closes the list for everybody. Keep the check cheap for the same reason. What somebody may do inside a tool they may open stays the tool's own if: user() tells it who is asking.
A file name is an address, and "Not allowed" is another answer than "There is no tool at this address". So whoever is logged in can find out that a tool with a certain name is there, without seeing anything of it. Name the files with that in mind.
Variables for the tools. A tool is a script of its own, so it sees no variable of yours. variables: hands some over by name:
Runio\ScriptUi\serve(
__DIR__ . '/../tools',
variables: ['orders' => $orderRepository, 'mailer' => $mailer],
);Every tool of the folder then starts with $orders and $mailer. An object is the object itself. Everything else is a copy that a tool may change for itself. A line such as /** @var OrderRepository $orders */ at the top of a tool tells your editor about the variable.
By default tools are chosen with ?_tool=refund. With base: '/admin/tools' they get clean addresses such as /admin/tools/refund (send those addresses to the script in your web server configuration). The list is at the base as you write it: at /admin/tools, or at /admin/tools/ when the base ends with a slash.
Inside Symfony and Laravel
serve() sends the page itself. A framework wants to send it: a controller returns a response, and the framework adds its cookies and headers on the way out. respond() takes everything serve() takes. It works out the same page and hands it back, with ->status, ->headers and ->body. One route leads every address under base: to it. The folder of tools lies next to src/ or app/, outside the web root.
Symfony:
// src/Controller/ToolsController.php
use function Runio\ScriptUi\respond;
final class ToolsController extends AbstractController
{
#[Route('/admin/tools/{tool}', defaults: ['tool' => ''], methods: ['GET', 'POST'])]
public function tools(Request $request): Response
{
$page = respond(
$this->getParameter('kernel.project_dir') . '/tools',
base: '/admin/tools',
auth: fn () => $this->getUser(),
allow: fn ($user, string $tool) => $tool !== 'payroll' || $this->isGranted('ROLE_PAYROLL'),
session: $request->getSession(),
);
return new Response($page->body, $page->status, $page->headers);
}
}Laravel:
// routes/web.php
use function Runio\ScriptUi\respond;
Route::match(['get', 'post'], '/admin/tools/{tool?}', function (Request $request) {
$page = respond(
base_path('tools'),
base: '/admin/tools',
auth: fn () => $request->user(),
allow: fn ($user, string $tool) => Gate::forUser($user)->allows('open-tool', $tool),
session: $request->session(),
);
return response($page->body, $page->status, $page->headers);
})->block();// bootstrap/app.php, inside withMiddleware()
$middleware->validateCsrfTokens(except: ['admin/tools', 'admin/tools/*']);Pass the status and all of $page->headers on. They belong to the answer: where a pressed button leads on to, the name and the kind of a download, the order not to keep the page, and the rule that lets the script of the page run.
The session. With session: the tools keep what they keep in the session of your application, under the one key _script_ui, and a logout ends it. Without it they use PHP's own session. In Laravel that is a second session next to Laravel's, and a logout of Laravel does not end it.
The session needs room. What one tool keeps of its inputs is a few kilobytes as a rule and 256 KB at most, and a session remembers 50 tools. A session that lives in a cookie, such as Laravel's cookie driver, is too small: a cookie holds about 4 KB. A database column of 64 KB can run full too. The BLOB that Symfony's PdoSessionHandler creates on MySQL is one, and Symfony names MEDIUMBLOB for more.
The user. auth: returns the user object of the framework, and allow: gets that object. user() and the log name it by getUserIdentifier() in Symfony and by getAuthIdentifier() in Laravel, which is the key of the row unless your model says otherwise. To show an email instead, return it: auth: fn () => $request->user()?->email. Then allow: gets the email too. What a tool keeps is bound to that name, so a name that changes, such as an email, starts from nothing once. For the same reason every place that serves tools in one session has to name a user the same way, and two kinds of users that share a number, such as admin 1 and customer 1, need names that differ.
Laravel checks every form for its own token. The forms of the tools carry the token of the library, and the library checks it on every request. So the route is taken out of Laravel's check, with the line for bootstrap/app.php above. Without it Laravel 12 answers every pressed button with "419 Page Expired". Laravel 13 lets a button through when the browser says that the request comes from the same site. Browsers say that on HTTPS and on localhost only, so the line is needed there as well. The line takes every address under admin/tools out of Laravel's check, so keep those addresses for the tools alone.
Laravel does not lock its session. Two requests of one user at the same moment each write the whole session, and the later one wins. What a tool kept in the other is lost then, for instance when a page that refreshes itself and a click meet. ->block() lets the second wait for the first. It needs a cache that can lock, such as file, database or Redis. Symfony keeps its session in PHP's own by default, which locks.
Errors. The error handling of the framework stays in charge. Laravel makes an error of every PHP warning, and Symfony does in its dev environment: a warning in a tool then ends the run, and the page says that the tool failed. After a fatal error the framework shows its own error page. What the tool had printed with echo by then stands in front of it.
End a run with stop(), not with exit. exit ends the framework together with the tool. The page is still sent and what the tool keeps is saved, but nothing of the framework runs after that line. In Laravel the lock of ->block() is not given back, and the next click of that user waits until it runs out, which is 10 seconds.
A file that a tool sends by itself. Hand files to download(). A tool may also close its page with one ob_end_clean() and print an answer of its own, and respond() hands that back with its status and headers. It holds all of the answer in memory, so it cannot stream: give a large file a route of its own. A tool that closes every output buffer, as in while (ob_get_level()) ob_end_clean();, sends its answer past respond() and past the framework, without the cookies and headers of your application. The error log says so.
Another Content-Security-Policy. The page brings its own. A browser applies every policy it gets, so a second one that your application adds to all its responses blocks the script and the styles of the page. Leave this route out of it.
What does not work. respond() takes the request from PHP itself ($_SERVER, $_GET, $_POST and $_FILES). The test client of a framework does not fill those in, so a tool cannot be opened with $client->request() or $this->get(). respond() says so with an exception. Try a tool in a browser test. Servers that keep PHP running between requests, such as Laravel Octane, RoadRunner, Swoole and FrankenPHP in worker mode, are not supported.
What was tried. Laravel 12 and 13 and Symfony 7.4 and 8.1, as they are after a fresh install, on PHP's built-in web server. The suite in e2e/frameworks runs the same tools inside each of them. Not tried with a framework: php-fpm, Apache and nginx, Symfony's prod environment, and a user that comes from a database.
Before colleagues get in
Go through the checklist: a login check, the folder outside the web root, who may open which tool, and a log.
Next: A table of 100,000 rows.