Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cli Router Laravel Package

litvinab/cli-router

cli-router is a lightweight PHP package for defining and dispatching command-line routes. Map CLI commands to handlers with parameters and simple route matching, making it easier to build structured console tools in Laravel or standalone PHP.

View on GitHub
Deep Wiki
Context7

Bundle fixes router URL generation when it used via CLI

Frequently asked questions about Cli Router
How do I install litvinab/cli-router?
Install it with Composer using composer require litvinab/cli-router, then include Composer's autoloader in your CLI entry point. The package is framework-agnostic and has no extra dependencies, so no additional libraries need to be pulled in. There is no mandatory configuration file or service provider step described for getting started.
Can I use cli-router outside of Laravel in a plain PHP project?
Yes. The package is framework-agnostic and does not depend on Laravel or any other framework, so it works in any PHP project that loads Composer's autoloader. You register your routes and dispatch the terminal input from a standalone PHP script. Laravel is simply one possible host for that script, not a requirement.
What problem does cli-router solve compared to a switch statement?
cli-router replaces the growing switch or if/else chain that usually accumulates in a CLI entry point as commands are added. You associate each command name with a closure or handler class, and the router dispatches incoming terminal input to the right one. This keeps the entry point small and moves command logic into separate, testable units.
How does cli-router capture command arguments?
The router extracts positional and optional parameters from the terminal input and passes them to the matched handler. That means commands can accept required arguments plus optional ones without you hand-parsing the argv array. Argument handling lives in the route definition rather than in every command body.
How do I define a default command for unmatched input?
cli-router includes fallback handling: you can register a default command that runs when no route matches the input. This is useful for showing a help screen, a usage summary, or a friendly error instead of a stack trace. It also keeps the behaviour predictable for users who mistype a command name.
Can I route a command to a handler class instead of a closure?
Yes. Commands can be mapped either to closures or to dedicated handler classes, so you can choose the lighter option for trivial commands and a class for anything that deserves its own file. Handler classes make it easier to unit test command logic and to keep a large CLI tool organised as it grows.
Is cli-router a replacement for Laravel Artisan commands?
No. Artisan already provides command registration through command classes and signatures, so cli-router is aimed at custom PHP entry points rather than at replacing Artisan. It is most useful when you want a small routing layer in a standalone console script or a bespoke Laravel-side CLI tool. If you are happy with Artisan's command classes, you likely do not need it.
Which Laravel versions does litvinab/cli-router support?
Because the package is framework-agnostic and introduces no framework dependencies, it is not tied to a specific Laravel release. It runs anywhere your PHP version and Composer autoloader are satisfied. Check the package's composer.json and release notes on the repository for the exact PHP version constraint before adopting it.
How do I test CLI commands built with cli-router?
Dispatching is plain PHP, so you can invoke the router from PHPUnit or any other test runner by supplying the input string and asserting the resulting output or handler behaviour. Routing commands to handler classes makes this easier, since each command can be tested in isolation. Testing the fallback route as well confirms unmatched input is handled gracefully.
Is cli-router production ready and what are the alternatives?
It is a small, single-purpose package with no external dependencies, which keeps the risk surface low, but you should review the repository's recent commits, releases, and open issues to judge maintenance activity for your project. Popular alternatives for PHP console tooling include Symfony Console, Laravel's Artisan command classes, and lightweight micro-frameworks such as Minicli. cli-router is a good fit when you want simple command-to-handler mapping without a heavier console framework.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource