Skip to content

PHP and Laravel to Ruby

Recorder: the polyglot PHP adapter (functions and static methods are called through a generated driver). Laravel routes (Route::get) are found by analyze.

Laravel to Rails (they are close cousins)

Laravel Rails
routes/web.php, routes/api.php config/routes.rb (keep the /api prefix)
Controller + Request $request controller action + params
return response()->json($x, 201) render json: x, status: :created
abort(404) / firstOrFail() head :not_found / find_by! + rescue_from with the recorded body
Blade (@extends, @section, @foreach, {{ }}) ERB layouts (yield/content_for), each, <%= %> (frontend.md)
Eloquent model, relations, scopes, casts ActiveRecord model, associations, scopes, attribute/serializers
Migrations Rails migrations, but keep the existing tables first (ADR)
Form Requests / $request->validate([...]) model validations or a form object
Middleware before_action / Rack middleware
Queues / Jobs Active Job + Solid Queue
Mailables Action Mailer
config() / .env config/*.yml, credentials, ENV
Sanctum / Breeze auth Rails authentication generator; bcrypt hashes carry over ($2y$ -> $2a$)

Constructs

  • Errors: InvalidArgumentException -> ArgumentError; DivisionByZeroError -> ZeroDivisionError; RuntimeException -> RuntimeError; the app's own exceptions keep their names.
  • Arrays are ordered maps: a PHP array with keys 0..n-1 is a JSON list; with gaps (after unset or array_filter) it becomes a JSON object. Use array_values semantics (values) where the recording shows a list.
  • null -> nil; isset($a['k']) -> a.key?("k") && !a["k"].nil?; empty($x) is true for 0, "0", "", [], null.
  • Strings: strlen counts bytes (bytesize); mb_strlen counts characters (length). . concatenation -> interpolation.

Traps

  • Loose comparison (==, in_array without strict): "1" == 1, "abc" == 0 (PHP 7). Port the coercion explicitly where a recorded case depends on it.
  • Rounding: round(2.5) == 3 and round(-2.5) == -3 (half away from zero) like Ruby's round. intdiv truncates toward zero; % takes the dividend's sign (remainder); / returns an int when exact (4 / 2 === 2) and a float otherwise.
  • Int overflow turns into float silently.
  • json_encode escapes / as \/ and unicode as \uXXXX by default: the body text differs but the parsed JSON is equal (replay compares parsed JSON).
  • sort, usort are not stable before PHP 8.
Edit this page on GitHub →