Alpha
Alpha
AlphaBeta: when quoting is solved

The Basics

Routing

Basic Routing

The most basic Laravel Bash routes accept a method, a URI and the name of a function, which is a very simple and expressive way of defining routes and behaviour without complicated routing configuration files:

route GET /greeting greeting

greeting() {
  echo 'Hello World'
}

The Default Route Files

All routes are defined in routes/web.sh, which is sourced when the framework boots. Because every request is a new process, it's sourced for every request too. Route caching is not available, and also not needed, because there is nothing to cache it in.

Available Router Methods

route GET    /posts        PostController::index
route POST   /posts        PostController::store
route PUT    /posts/{post} PostController::update
route DELETE /posts/{post} PostController::destroy

Route Parameters

Required Parameters

Parameters are wrapped in braces, and passed to your function as arguments, in order:

route GET /posts/{post}/comments/{comment} show_comment

show_comment() {
  local post=$1 comment=$2
  echo "Post $post, comment $comment"
}

note

ShellCheck will tell you that {post} looks like brace expansion. It isn't: Bash only expands braces that contain a comma or a range, like {a,b} or {1..3}.

How matching works

route turns each {param} into the regex ([^/]+) and stores the result. For each request, router::dispatch tries them in order with [[ =~ ]], which puts the captured groups in BASH_REMATCH:

if [[ $REQUEST_PATH =~ ${ROUTE_REGEXES[i]} ]]; then
  "${ROUTE_ACTIONS[i]}" "${BASH_REMATCH[@]:1}"
fi

If the path matches but the method doesn't, the response is a 405. If nothing matches, it's a 404.

Controllers as functions

Bash allows :: in function names, so controllers keep their Laravel names with no extra work:

route GET /posts/{post} PostController::show

See Controllers.

Listing Your Routes

./sail artisan route:list
  GET    / ........................................... welcome
  GET    /examples ................................... examples
  GET    /dashboard .................................. DashboardController::index
  GET    /posts ...................................... PostController::index
  GET    /posts/create ............................... PostController::create
  POST   /posts ...................................... PostController::store throttle:10,1
  GET    /posts/{post} ............................... PostController::show

Middleware

Middleware goes after the action, as name:arguments. It runs before the action, and stops the request by calling abort:

route POST /posts PostController::store throttle:10,1

Each middleware is a function called middleware::<name>, in framework/middleware.sh or anywhere else that gets sourced. Its arguments arrive as one string, 10,1, the way Laravel passes them.

Rate Limiting

throttle:<max>,<minutes> allows each client <max> requests every <minutes> minutes, then answers 429 Too Many Requests with a Retry-After header. Each request appends one byte to a file under storage/framework/throttle, and the size of the file is the count:

printf . >> "$prefix-$window"
count=$(stat -c %s "$prefix-$window")

A one-byte write with O_APPEND, which is what >> asks for, is atomic, so two requests at the same moment can't both read 9 and both write 10. There's nothing to lock.

Behind a proxy, every request seems to come from the proxy. Set TRUSTED_PROXY_HEADER to the header your proxy fills in with the real address, and only if the proxy replaces that header rather than passing on whatever the visitor sent:

TRUSTED_PROXY_HEADER=X-Envoy-External-Address

Not available

Named routes, route groups, model binding and signed URLs. Pull requests are welcome, but they will need to be written in Bash.