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-1is a JSON list; with gaps (afterunsetorarray_filter) it becomes a JSON object. Usearray_valuessemantics (values) where the recording shows a list. - null ->
nil;isset($a['k'])->a.key?("k") && !a["k"].nil?;empty($x)is true for0,"0","",[],null. - Strings:
strlencounts bytes (bytesize);mb_strlencounts characters (length)..concatenation -> interpolation.
Traps
- Loose comparison (
==,in_arraywithout strict):"1" == 1,"abc" == 0(PHP 7). Port the coercion explicitly where a recorded case depends on it. - Rounding:
round(2.5) == 3andround(-2.5) == -3(half away from zero) like Ruby'sround.intdivtruncates 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_encodeescapes/as\/and unicode as\uXXXXby default: the body text differs but the parsed JSON is equal (replay compares parsed JSON).sort,usortare not stable before PHP 8.