A collection of Rails-esque helper functions for use in your PHP project.
This project will attempt to bring over some useful helper methods from Rails (specifically ActionView)
In your composer.json
, add this:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Memerator/RailsHelpers-PHP.git"
}
]
}
Then add the dependency:
{
"require": {
"memerator/rails-helpers": "dev-main"
}
}
Finally, composer update
(or composer update memerator/rails-helpers
)
Once installed, the functions are global, so reference them as you would Rails.
Ruby:
link_to "Rory!", "https://rory.cat"
PHP:
link_to("Rory!", "https://rory.cat")
PHP 8.0 introduced Named Arguments, which seem a lot like Ruby:
function my_func(string $name, string $data = null, string $extra = []) {
// fanciness
}
my_func("Hello!", data: "Interesting", extra: ["woah"]);
Unfortunately, unlike Ruby, we could not utilize this in a dynamic variadic way:
function my_tag(string $name, string $body = null, array $options = [], ...$variadic) {
// tag code here!
}
my_tag("span", "Woah there", id: "Calm down!");
While PHP would let us run this, we are unable to utilize the extra objects. In fact, their values don't even show up.
The closest we can get is using the $options as an array, as such:
tag("span", "Woah there!", ["id" => "Calm down!"]);
results in
<span id="Calm down!">Woah there!</span>
whereas Ruby would look like:
tag.span, "Woah there", id: "Calm down!"
Unfortunately, it makes the PHP code longer than its HTML counterpart, but this is currently unavoidable.