Skip to content
This repository has been archived by the owner on Apr 6, 2024. It is now read-only.
/ RailsHelpers-PHP Public archive

A collection of Rails-esque helper functions for use in your PHP project

License

Notifications You must be signed in to change notification settings

Memerator/RailsHelpers-PHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RailsHelpers for PHP

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)

Installing

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)

Using

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")

Named Arguments?

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.

About

A collection of Rails-esque helper functions for use in your PHP project

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages